imageView

来源:互联网 发布:coc国王升级数据 编辑:程序博客网 时间:2024/05/01 10:15

参考:http://guides.codepath.com/android/Working-with-the-ImageView#scaling-a-bitmap
图片制定宽度没有制定高度:

<ImageView    android:layout_width="50dp"    android:layout_height="wrap_content"    android:scaleType="fitXY"    android:adjustViewBounds="true"    .../>

动态设置图片宽和高:

imageView.getLayoutParams().height = 100;imageView.getLayoutParams().width = 100;

scaleType的各种类型:
center:在视图中间展示图片不缩放
centerCrop:保持宽高比缩放整个图片使图片充满整个视图
centerInside:缩小视图使图片居于视图内。
fitCenter:缩小视图,使其至少一条边匹配视图
fitStart:fitEnd:缩放视图在坐上或右下显示
fitxy:图片铺满视图无视宽高比
适配各种设备的图片像素比应为:8:6:4:3:2
这是一个第三方工具可以方便的获取各种精度的图片:我已下载
https://github.com/asystat/Final-Android-Resizer

处理BitMap

代码中设置源文件:
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test2);
或者通过文件夹目录:
BitMap bMap = BitMapFactory.decodeFile(“/sdcard/test2.png”);
image.setImageBitmap(bMap);
缩放BitMap:

//从drawable文件夹中获取bitmapBitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);//设置bitmap文件宽和高BitMap bMapScaled = BitMap.createScaledBitMap(bMap, 150,100,true);//向ImageView中加载BitMapImageView image = (ImageView) findViewById(R.id.test_image);iamge.setImageBitmap(bMapScaled);

创建一个BitmapScaler utility类来设置文件

ublic class BitmapScaler{    // Scale and maintain aspect ratio given a desired width    // BitmapScaler.scaleToFitWidth(bitmap, 100);    public static Bitmap scaleToFitWidth(Bitmap b, int width)    {        float factor = width / (float) b.getWidth();        return Bitmap.createScaledBitmap(b, width, (int) (b.getHeight() * factor), true);    }    // Scale and maintain aspect ratio given a desired height    // BitmapScaler.scaleToFitHeight(bitmap, 100);    public static Bitmap scaleToFitHeight(Bitmap b, int height)    {        float factor = height / (float) b.getHeight();        return Bitmap.createScaledBitmap(b, (int) (b.getWidth() * factor), height, true);
0 0
原创粉丝点击