본문 바로가기

나 어제 배웠다/Android

Android Widget[Component]-ImageView

▣ Android Widget[Component] - ImageView

 

1. Project 생성 

  

2. main.xml widget 추가

 

 

 

 

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
  <ImageView
    android:id="@+id/iv01"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/ib"/>
</LinearLayout>

 

이미지 Resource 추가

 

 

 

 

3. 실행결과

 

 

※ 터치기능을 이용하여 이미지 변환하기

 ImageViewActivity.java 변경

 

 

package soo.ui.simple;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class ImageViewActivity extends Activity {
    /** Called when the activity is first created. */
 ImageView iv;
 //이미지 객체 배열
 final static int imgs[] = {R.drawable.ib, R.drawable.ib2};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        iv = (ImageView)findViewById(R.id.iv01);
        iv.setOnTouchListener(new View.OnTouchListener() {

   int i=0;
   public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub
    
    //iv.setImageResource(R.drawable.ib2);
    if(i>1) i=0;
    
    iv.setImageResource(imgs[i++]);
    
    return false;
   }
  });

    }
}

 

실행

마우스로 터치 - 1

 

 마우스로 터치 - 2

 반복적으로(터치이벤트가발생) 이미지가 변경