package cn.class3g.activity; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; public class ImageViewDemo extends Activity implements OnTouchListener { ImageView imageView1, imageView2; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.imageview_layout); findViews(); } private void findViews() { imageView1 = (ImageView) findViewById(R.id.img1); imageView2 = (ImageView) findViewById(R.id.img2); //为imageView添加触摸监听事件 imageView1.setOnTouchListener(this); } public boolean onTouch(View v, MotionEvent event) { float scale = 412 / 320; //获取需要显示的图片的开始点 int x = (int) (event.getX() * scale); int y = (int) (event.getY() * scale); //需要考虑边界问题 int width = (int) (100 * scale); int height = (int) (100 * scale); //获取图片显示框中的位图 BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView1.getDrawable(); //显示图片指定的区域 imageView2.setImageBitmap(Bitmap.createBitmap(bitmapDrawable.getBitmap(), x,y, width, height)); return false; } }
Android UI之ImageView图片视图
内容版权声明:除非注明,否则皆为本站原创文章。