* ImageView의 이미지 변경
ⓐ 변경할 ImageView선택
: onView = (ImageView)findViewById(R.id.onView);
ⓑ 원하는 이미지 정보 추출
: BitmapDrawable img = (BitmapDrawable)getResources().getDrawable(R.drawable.img1);
// drawable폴더에서 img1의 정보를 가져옴
ⓒ View에 이미지 setting
: onView.setImageDrawable(img);
※ 이미지 삭제시 : onView.setImageDrawable(null);
-- Exam1Activity.java
-- main.xml
</LinearLayout>
ⓐ 변경할 ImageView선택
: onView = (ImageView)findViewById(R.id.onView);
ⓑ 원하는 이미지 정보 추출
: BitmapDrawable img = (BitmapDrawable)getResources().getDrawable(R.drawable.img1);
// drawable폴더에서 img1의 정보를 가져옴
ⓒ View에 이미지 setting
: onView.setImageDrawable(img);
※ 이미지 삭제시 : onView.setImageDrawable(null);
-- Exam1Activity.java
package exam.exam1;
import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Exam1Activity extends Activity {
/** Called when the activity is first created. */
private boolean index = true;
private ImageView onbtn;
private BitmapDrawable dr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.btn);
onbtn = (ImageView)findViewById(R.id.onbtn);
dr = (BitmapDrawable)getResources().getDrawable(R.drawable.img1);
btn.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if (index){
onbtn.setImageBitmap(null);
index = false;
}else{
onbtn.setImageDrawable(dr);
index = true;
}
}
});
}
} -- main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_marginTop="10dp"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/onbtn"
android:src="@drawable/img1"
/>
</ScrollView>
</HorizontalScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn"
android:text="toggle"
android:layout_gravity="center"
/>