Android 如何从库中调整图像大小?
第一个文件允许用户从图库中选择一张照片,并将其发送给一个将处理该照片的活动。问题是图像对于手机屏幕来说太大了,所以你只能看到图像的上角(就像被放大了一样) 。第二个文件是从intent接收图像数据并将其放置在位图派生的URI中的活动Android 如何从库中调整图像大小?,android,bitmap,android-contentprovider,Android,Bitmap,Android Contentprovider,第一个文件允许用户从图库中选择一张照片,并将其发送给一个将处理该照片的活动。问题是图像对于手机屏幕来说太大了,所以你只能看到图像的上角(就像被放大了一样) 。第二个文件是从intent接收图像数据并将其放置在位图派生的URI中的活动 public class DisplayUndistortedBitmapFromGalleryActivity extends Activity { private static final String TAG = "*********DUBFGAct
public class DisplayUndistortedBitmapFromGalleryActivity extends Activity {
private static final String TAG = "*********DUBFGActivity";
private Context mContext = this;
Uri uri;
private Bitmap mbitmap = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
uri = getIntent().getData();
if(uri != null){
Log.e(TAG, "uri ok");
}else {
Log.e(TAG, "uri not ok");
}
try {
mbitmap = Media.getBitmap(getContentResolver(), uri);
//setMbitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
我有第三个文件,它是活动的customview。下面的行从位图的活动中检索位图并显示它。有没有办法缩小位图,使其适合屏幕?谢谢
Bitmap bm = ((DisplayUndistortedBitmapFromGalleryActivity)getContext()).getMbitmap();
下面是我通常如何调整位图的大小,这是最简单的方法
public class bitmaptest extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable
编辑:
这是另一个选择
int targetWidth = bitmapOrg.getWidth() - 15; //change this to control the size
int targetHeight = bitmapOrg.getHeight() - 15 ;
Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, targetWidth, targetHeight, matrix, true);
下面是我通常如何调整位图的大小,这是最简单的方法
public class bitmaptest extends Activity {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout linLayout = new LinearLayout(this);
// load the origial BitMap (500 x 500 px)
Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
R.drawable.android);
int width = bitmapOrg.width();
int height = bitmapOrg.height();
int newWidth = 200;
int newHeight = 200;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// rotate the Bitmap
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,
width, height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable
编辑:
这是另一个选择
int targetWidth = bitmapOrg.getWidth() - 15; //change this to control the size
int targetHeight = bitmapOrg.getHeight() - 15 ;
Matrix matrix = new Matrix();
matrix.postScale(1f, 1f);
Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, targetWidth, targetHeight, matrix, true);
我使用的不是ImageView,而是customView。我在活动中使用以下代码行来获取位图。mbitmap=Media.getBitmap(getContentResolver(),uri);我只需要缩小它的大小,但我找不到在位图或bitmapFactory类中使用uri来实现这一点的方法。这里还有更简单的bitmap.createScaledBitmap()。我使用的不是ImageView,而是customView。我在活动中使用以下代码行来获取位图。mbitmap=Media.getBitmap(getContentResolver(),uri);我只需要缩小它的大小,但我找不到在位图或bitmapFactory类中使用uri来实现这一点的方法。这里还有更简单的bitmap.createScaledBitmap()。