Androidは非ローカル画像のクリック効果を実現

2842 ワード

mainActivityは次のとおりです.
package cn.c;
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
/**
 *     :
 * ImageView      ,   ImageView          
 *             
 *     :
 *   ImageView  OnTouchListener()       
 *         ImageView   Alpha
 *     :
 * onTouch() return true;         
 *        
 * 
 */
public class MainActivity extends Activity {
   private ImageView mImageView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
    private void init(){
    	mImageView=(ImageView) findViewById(R.id.imageView);
    	//         ,  
    	//mImageView.setImageBitmap(bitmap from network);
    	mImageView.setOnTouchListener(new TouchListenerImpl());
    }
    private class TouchListenerImpl implements OnTouchListener{
		public boolean onTouch(View v, MotionEvent event) {
			ImageView imageView=(ImageView) v;
			//  
			if (event.getAction()==MotionEvent.ACTION_DOWN) {
				System.out.println("down down down ");
				imageView.setAlpha(0);
				imageView.invalidate();
			}
			
			//  
			if (event.getAction()==MotionEvent.ACTION_UP||
			   event.getAction()==MotionEvent.ACTION_CANCEL) {
				System.out.println("up up up ");
				imageView.setAlpha(200);
				imageView.invalidate();
			}
			return true;
		}
    } 
}

main.xmlは次のとおりです.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ImageView        ,   ImageView          .            "
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
    />
   
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android:src="@drawable/ic_launcher"
        android:layout_centerInParent="true"
     />

</RelativeLayout>