首页 > 代码库 > ImageView Tool

ImageView Tool

package com.example.imgview;import android.support.v7.app.ActionBarActivity;import android.support.v7.app.ActionBar;import android.support.v4.app.Fragment;import android.os.Bundle;import android.view.LayoutInflater;import android.view.Menu;import android.view.MenuItem;import android.view.MotionEvent;import android.view.View;import android.view.View.OnClickListener;import android.view.View.OnTouchListener;import android.view.ViewGroup;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;import android.os.Build;public class MainActivity extends ActionBarActivity {    //this is global method     @Override    public boolean onTouchEvent(MotionEvent event) {         TextView tv = (TextView) findViewById(R.id.textView1);                     int x = (int) event.getX();            int y  = (int) event.getY();            tv.setText("X:"+x+"  Y:"+y);        return super.onTouchEvent(event);    }    int count = 0 ;    //store image     int[] pic = {            R.drawable.s1,            R.drawable.s2,            R.drawable.s3,            R.drawable.s4,    };        @Override    protected void onCreate(Bundle savedInstanceState) {        //get full screen width //        this.getWindowManager().getDefaultDisplay().getWidth();        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final ImageView iv1 =(ImageView) findViewById(R.id.imageView1);        if (savedInstanceState == null) {            getSupportFragmentManager().beginTransaction()                    .add(R.id.container, new PlaceholderFragment()).commit();        }        Button bnex = (Button) findViewById(R.id.bnex);        Button bpri = (Button) findViewById(R.id.bpri);                bnex.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                ImageView iv = (ImageView) findViewById(R.id.imageView1);                count-- ;                if(count<=0){                    count = 3 ;                }                iv.setImageResource(pic[count]);            }        });        bpri.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                ImageView iv = (ImageView) findViewById(R.id.imageView1);                count++ ;                if(count>=3){                    count = 0 ;                }                iv.setImageResource(pic[count]);            }        });        iv1.setOnTouchListener(new OnTouchListener() {                        @Override            public boolean onTouch(View arg0, MotionEvent arg1) {                 if(arg1.getAction()==MotionEvent.ACTION_DOWN){                     if(count>=3)count  =  0 ;                     count++;                     iv1.setImageResource(pic[count]);                 }else if(arg1.getAction()==MotionEvent.ACTION_UP)                 {                     count-- ;                     if(count<=0)count = 3 ;                     iv1.setImageResource(pic[count]);                 }                return false;            }        });                            }        @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }    @Override    public boolean onOptionsItemSelected(MenuItem item) {        // Handle action bar item clicks here. The action bar will        // automatically handle clicks on the Home/Up button, so long        // as you specify a parent activity in AndroidManifest.xml.        int id = item.getItemId();        if (id == R.id.action_settings) {            return true;        }        return super.onOptionsItemSelected(item);    }    /**     * A placeholder fragment containing a simple view.     */    public static class PlaceholderFragment extends Fragment {        public PlaceholderFragment() {        }        @Override        public View onCreateView(LayoutInflater inflater, ViewGroup container,                Bundle savedInstanceState) {            View rootView = inflater.inflate(R.layout.fragment_main, container,                    false);            return rootView;        }    }}