Button x2(Up, Down)
ImageView x2
ScrollView x2
구현하려하는 것은 이미지뷰를 위아래로 배치한 후
버튼을 눌렀을 때 이미지의 위치를 변경
ImageView imageView;ImageView imageView2;BitmapDrawable bitmap;
이미지뷰 아이디로 이벤트를 실행 시킬 것이기 때문에
이미지뷰 객체를 2개 생성.
imageView = findViewById(R.id.imageView);
imageView2 = findViewById(R.id.imageView2);
이미지뷰의 아이디로 뷰를 찾음
public void onButton1Clicked(View v){
upImage();}
public void onButton2Clicked(View v){
downImage();}
버튼 클릭 이벤트가 발생했을 때
upImage와 downImage 메소드가 실행 된다.
private void upImage(){
Resources res = getResources();
bitmap = (BitmapDrawable)res.getDrawable(R.drawable.youtube);
int bitmapWidth = bitmap.getIntrinsicWidth();
int bitmapHeight = bitmap.getIntrinsicHeight();
imageView2.setImageDrawable(null);
imageView.setImageDrawable(bitmap);
imageView.getLayoutParams().width = bitmapWidth;
imageView.getLayoutParams().height = bitmapHeight;}
이미지를 위로 올리는 메소드는 아래의 이미지뷰를 먼저 비워주고
위의 이미지뷰에 다시 그림을 그려주는 방식을 사용한다.
아래로 내리는 방식도 동일하다.
private void downImage(){
Resources res = getResources();
bitmap = (BitmapDrawable)res.getDrawable(R.drawable.youtube);
imageView.setImageDrawable(null); imageView2.setImageDrawable(bitmap);
}
Resources res = getResources();
bitmap = (BitmapDrawable)res.getDrawable(R.drawable.youtube);
imageView.setImageDrawable(null); imageView2.setImageDrawable(bitmap);
}
간단한 방법으로 이미지의 위치를 바꾸는 것을 구현했다.
교재 : 안드로이드 웹프로그래밍
댓글 없음:
댓글 쓰기