Android2.3使用BitmapRegionDecoder獲取指定區域的圖片
阿新 • • 發佈:2019-02-14
publicclass DisplayImageRegionActivity extends Activity implements OnTouchListener {
privatefinal Rect mRect =new Rect();
private BitmapRegionDecoder mDecoder;
private ImageView mView;
@Override
publicvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView (R.layout.main);
mView =new ImageView(this);
mView.setAdjustViewBounds(true);
mView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mView.setScaleType(ScaleType.CENTER);
mView.setOnTouchListener(this);
setContentView(mView );
try{
InputStream is = getResources().openRawResource(R.drawable.a);
mDecoder = BitmapRegionDecoder.newInstance(is, true);
}catch(IOException e){
e.printStackTrace();
}
}
@Override
publicboolean onTouch(View v, MotionEvent event){
finalint action = event.getAction()& MotionEvent. ACTION_MASK;
finalint x =(int) event.getX();
finalint y =(int) event.getY();
switch(action){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
setImageRegion(x, y);
break;
}
returntrue;
}
privatevoid setImageRegion(int left, int top){
// BitmapFactory.Options opts = new BitmapFactory.Options();
finalint width = mView.getWidth();
finalint height = mView.getHeight();
finalint imgWidth = mDecoder.getWidth();
finalint imgHeight = mDecoder.getHeight();
int right = left + width;
int bottom = top + height;
if(right > imgWidth) right = imgWidth;
if(bottom > imgHeight) bottom = imgHeight;
if(left <0) left =0;
if(top <0) top =0;
mRect.set(left, top, right, bottom);
Bitmap bm = mDecoder.decodeRegion(mRect, null);
mView.setImageBitmap(bm);
}
}
privatefinal Rect mRect =new Rect();
private BitmapRegionDecoder mDecoder;
private ImageView mView;
@Override
publicvoid onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView
mView =new ImageView(this);
mView.setAdjustViewBounds(true);
mView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mView.setScaleType(ScaleType.CENTER);
mView.setOnTouchListener(this);
setContentView(mView
try{
InputStream is = getResources().openRawResource(R.drawable.a);
mDecoder = BitmapRegionDecoder.newInstance(is, true);
}catch(IOException e){
e.printStackTrace();
}
}
@Override
publicboolean onTouch(View v, MotionEvent event){
finalint action = event.getAction()& MotionEvent.
finalint x =(int) event.getX();
finalint y =(int) event.getY();
switch(action){
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
setImageRegion(x, y);
break;
}
returntrue;
}
privatevoid setImageRegion(int left, int top){
// BitmapFactory.Options opts = new BitmapFactory.Options();
finalint width = mView.getWidth();
finalint height = mView.getHeight();
finalint imgWidth = mDecoder.getWidth();
finalint imgHeight = mDecoder.getHeight();
int right = left + width;
int bottom = top + height;
if(right > imgWidth) right = imgWidth;
if(bottom > imgHeight) bottom = imgHeight;
if(left <0) left =0;
if(top <0) top =0;
mRect.set(left, top, right, bottom);
Bitmap bm = mDecoder.decodeRegion(mRect, null);
mView.setImageBitmap(bm);
}
}