标签:比较 bit roi tor pen rri style || ext
BitmapFactory.Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.bigpicture, options);
int outHeight = options.outHeight;
int outWidth = options.outWidth;
BitmapFactory.Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.bigpicture, options);
int outHeight = options.outHeight;
int outWidth = options.outWidth;
int inSampleSize = 1;
int height = view.getMeasuredHeight();
int width = view.getMeasuredWidth();
if (outHeight > height || outWidth > width) {
int halfHeight = outHeight / 2;
int halfWidth = outWidth / 2;
while ((halfHeight / inSampleSize) >= height || (halfWidth / inSampleSize) >= width) {
inSampleSize *= 2;
}
}
private void loadImage(ImageView view) {
BitmapFactory.Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.bigpicture, options);
int outHeight = options.outHeight;
int outWidth = options.outWidth;
int inSampleSize = 1;
int height = view.getMeasuredHeight();
int width = view.getMeasuredWidth();
if (outHeight > height || outWidth > width) {
int halfHeight = outHeight / 2;
int halfWidth = outWidth / 2;
while ((halfHeight / inSampleSize) >= height || (halfWidth / inSampleSize) >= width) {
inSampleSize *= 2;
}
}
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bigpicture, options);
view.setImageBitmap(bitmap);
}
InputStream inputStream = getResources().getAssets().open("bigpicture.jpg");
BitmapRegionDecoder regionDecoder = BitmapRegionDecoder.newInstance(inputStream, false);
int measuredHeight = view.getMeasuredHeight();
int measuredWidth = view.getMeasuredWidth();
Rect rect = new Rect(0, 0, measuredWidth, measuredHeight);
Bitmap bitmap = regionDecoder.decodeRegion(rect, new Options());
view.setImageBitmap(bitmap);
以上就可以在 ImageView 中展示图片的局部。
public class BigImageView extends View {
private int slideX = 0;
private int slideY = 0;
private BitmapRegionDecoder bitmapRegionDecoder;
private Paint paint;
private int mImageWidth;
private int mImageHeight;
private Options options;
private Rect mRect;
public BigImageView(Context context) {
this(context, null);
}
public BigImageView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public BigImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
mRect = new Rect();
paint = new Paint();
try {
InputStream inputStream = getResources().getAssets().open("bigpicture.jpg");
options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);
mImageWidth = options.outWidth;
mImageHeight = options.outHeight;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Config.RGB_565;
bitmapRegionDecoder = BitmapRegionDecoder.newInstance(inputStream, false);
} catch (IOException e) {
e.printStackTrace();
}
}
float downX = 0;
float downY = 0;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
downX = event.getRawX();
downY = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
float moveX = event.getRawX();
float moveY = event.getRawY();
slideX = (int) (moveX - downX);
slideY = (int) (moveY - downY);
if (mImageWidth > getWidth()) {
mRect.offset(-slideX, 0);
if (mRect.right > mImageWidth) {
mRect.right = mImageWidth;
mRect.left = mRect.right - getWidth();
}
if (mRect.left < 0) {
mRect.left = 0;
mRect.right = getWidth();
}
invalidate();
}
if (mImageHeight > getHeight()) {
mRect.offset(0, -slideY);
if (mRect.bottom > mImageHeight) {
mRect.bottom = mImageHeight;
mRect.top = mRect.bottom - getHeight();
}
if (mRect.top < 0) {
mRect.top = 0;
mRect.bottom = getHeight();
}
invalidate();
}
downX = moveX;
downY = moveY;
break;
default:
break;
}
return true;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mRect.left = 0;
mRect.right = getMeasuredWidth() + mRect.left;
mRect.top = 0;
mRect.bottom = getMeasuredHeight() + mRect.top;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Bitmap bitmap = bitmapRegionDecoder.decodeRegion(mRect, options);
canvas.drawBitmap(bitmap, 0, 0, paint);
}
}
标签:比较 bit roi tor pen rri style || ext
原文地址:https://www.cnblogs.com/liyiran/p/9272880.html