图片比例缩放以及bitMap转BitmapDrawable
16lz
2022-03-15
import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Matrix;import android.graphics.drawable.BitmapDrawable;import android.os.Bundle;import android.view.ViewGroup.LayoutParams;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.ImageView.ScaleType;/** * This example shows how to resize an image * @author FaYnaSoft Labs * */public class Main extends Activity {@Overridepublic void onCreate(Bundle icicle) {super.onCreate(icicle);LinearLayout linearLayout = new LinearLayout(this);Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);int width = bitmap.getWidth();int height = bitmap.getHeight();int newWidth = 640;int newHeight = 480;float scaleWidth = ((float) newWidth) / width;float scaleHeight = ((float) newHeight) / height;Matrix matrix = new Matrix();matrix.postScale(scaleWidth, scaleHeight);// create the new Bitmap objectBitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,height, matrix, true);BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);ImageView imageView = new ImageView(this);imageView.setImageDrawable(bmd);imageView.setScaleType(ScaleType.CENTER);linearLayout.addView(imageView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));setContentView(linearLayout);}}
更多相关文章
- 代码中设置drawableleft
- android 3.0 隐藏 系统标题栏
- Android开发中activity切换动画的实现
- Android(安卓)学习 笔记_05. 文件下载
- Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
- 技术博客汇总
- android 2.3 wifi (一)
- AndRoid Notification的清空和修改
- Android中的Chronometer