ImageView 使用详解
极力推荐文章:欢迎收藏
Android 干货分享
阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android
本篇文章主要介绍 Android
开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:
一、ImageView 的继承关系
二、ImageView 常用方法
三、ImageView 背景 间距属性设置
四、使用Bitmap 类型动态设置ImageView 资源
五、ImageView 图片倒影实现
六、ImageView 图片缩放实现
七、ImageView 圆角图片实现
八、Bitmap 与Drawable 转换工具类
一、ImageView 的继承关系
ImageView
的继承关系 如下:
java.lang.Object
↳ android.view.View
↳ android.widget.ImageView
二、ImageView 常用方法
ImageView
主要用于显示图像资源,Bitmap
或Drawable
资源,同时也常用于图片渲染调色,图片缩放剪裁等。
以下XML
代码段是使用ImageView
显示图像资源的常见示例:
- 在
xml
使用ImageView
控件
- 在
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
三、 ImageView 背景 间距属性设置
- 在
xml
使用ImageView
控件
- 在
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/grey"
android:padding="5dp"
android:src="@drawable/ic_launcher" />
- 实现效果如下:
四、 使用Bitmap 类型动态设置ImageView 资源
- 在
xml
使用ImageView
控件
- 在
<ImageView
android:id="@+id/img_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
- 2.
java
类实现
// 1.从资源中获取Bitmap
ImageView mImageView1 = (ImageView) findViewById(R.id.img_1);
DrawableUtils.UseBitmap(this, mImageView1, R.drawable.gril);
- 3.
DrawableUtils
类方法实现
// 1.从资源中获取Bitmap
public static void UseBitmap(Context context, ImageView imageView, int drawableId) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
drawableId);
imageView.setImageBitmap(bitmap);
}
- 实现效果如下:
五、ImageView 图片倒影实现
- 在
xml
使用ImageView
控件
- 在
<ImageView
android:id="@+id/img_4"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@color/grey"
android:padding="5dp" />
java
代码 实现效果
// 4.倒影图片
ImageView mImageView4 = (ImageView) findViewById(R.id.img_4);
mImageView4.setImageBitmap(DrawableUtils.CreateReflectionImageWithOrigin(
DrawableUtils.DrawableToBitmap(getResources().getDrawable(
R.drawable.img1))));
DrawableUtils
工具类的方法实现
// 5. Drawable----> Bitmap
public static Bitmap DrawableToBitmap(Drawable drawable) {
// 获取 drawable 长宽
int width = drawable.getIntrinsicWidth();
int heigh = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, heigh);
// 获取drawable的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 创建bitmap
Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
// 创建bitmap画布
Canvas canvas = new Canvas(bitmap);
// 将drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
- 实现效果如下:
六、ImageView 图片缩放实现
- 在
xml
使用ImageView
控件
- 在
<ImageView
android:id="@+id/img_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
java
代码 实现效果
// 2. 图片缩放
ImageView mImageView2 = (ImageView) findViewById(R.id.img_2);
mImageView2.setImageDrawable(DrawableUtils.ZoomDrawable(getResources().getDrawable(R.drawable.img1),
240, 200));
DrawableUtils
工具类方法实现
// 9. drawable进行缩放 ---> bitmap 然后比对bitmap进行缩放
public static Drawable ZoomDrawable(Drawable drawable, int w, int h) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
// 调用5 中 drawable转换成bitmap
Bitmap oldbmp = DrawableToBitmap(drawable);
// 创建操作图片用的Matrix对象
Matrix matrix = new Matrix();
// 计算缩放比例
float sx = ((float) w / width);
float sy = ((float) h / height);
// 设置缩放比例
matrix.postScale(sx, sy);
// 建立新的bitmap,其内容是对原bitmap的缩放后的图
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
matrix, true);
return new BitmapDrawable(newbmp);
}
- 实现效果如下:
七、ImageView 圆角图片 实现
- 在
xml
使用ImageView
控件
- 在
<ImageView
android:id="@+id/img_3"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/grey"
android:padding="5dp" />
- 2.
java
代码 实现效果
// 3. 圆角图片
ImageView mImageView3 = (ImageView) findViewById(R.id.img_3);
mImageView3.setImageBitmap(DrawableUtils.SetRoundCornerBitmap(
DrawableUtils.DrawableToBitmap(getResources().getDrawable(
R.drawable.img1)), 60));
DrawableUtils
工具类方法实现
// 6.圆角图片
public static Bitmap SetRoundCornerBitmap(Bitmap bitmap, float roundPx) {
int width = bitmap.getWidth();
int heigh = bitmap.getHeight();
// 创建输出bitmap对象
Bitmap outmap = Bitmap.createBitmap(width, heigh,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outmap);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, heigh);
final RectF rectf = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectf, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return outmap;
}
- 实现效果如下:
八、Bitmap
与Drawable
转换工具类
Bitmap
与Drawable
转换常用工具类源代码如下:
package com.programandroid.Utils;
import java.io.ByteArrayOutputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;
/*
* DrawableUtils.java
*
* Created on: 2017-10-24
* Author: wangjie
*
* Welcome attention to weixin public number get more info
*
* WeiXin Public Number : ProgramAndroid
* 微信公众号 :程序员Android
*
*/
public class DrawableUtils {
// 1.从资源中获取Bitmap
public static void UseBitmap(Context context, ImageView imageView, int drawableId) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
drawableId);
imageView.setImageBitmap(bitmap);
}
// 2.Bitmap ---> byte[]
public byte[] BitmapToBytes(Bitmap bitmap) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
// 3.byte[] ---->bitmap
public Bitmap BytesToBitmap(byte[] b) {
if (b.length != 0) {
return BitmapFactory.decodeByteArray(b, 0, b.length);
} else {
return null;
}
}
// 4.Bitmap 缩放方法
public static Bitmap ZoomBitmap(Bitmap bitmap, int width, int heigh) {
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
float scalewidth = (float) width / w;
float scaleheigh = (float) heigh / h;
matrix.postScale(scalewidth, scaleheigh);
Bitmap newBmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
return newBmp;
}
// 5. Drawable----> Bitmap
public static Bitmap DrawableToBitmap(Drawable drawable) {
// 获取 drawable 长宽
int width = drawable.getIntrinsicWidth();
int heigh = drawable.getIntrinsicHeight();
drawable.setBounds(0, 0, width, heigh);
// 获取drawable的颜色格式
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565;
// 创建bitmap
Bitmap bitmap = Bitmap.createBitmap(width, heigh, config);
// 创建bitmap画布
Canvas canvas = new Canvas(bitmap);
// 将drawable 内容画到画布中
drawable.draw(canvas);
return bitmap;
}
// 6.圆角图片
public static Bitmap SetRoundCornerBitmap(Bitmap bitmap, float roundPx) {
int width = bitmap.getWidth();
int heigh = bitmap.getHeight();
// 创建输出bitmap对象
Bitmap outmap = Bitmap.createBitmap(width, heigh,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(outmap);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, width, heigh);
final RectF rectf = new RectF(rect);
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectf, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return outmap;
}
// 7.获取带倒影的图片
public static Bitmap CreateReflectionImageWithOrigin(Bitmap bitmap) {
final int reflectionGapLine = 4;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,
h / 2, matrix, false);
Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
canvas.drawBitmap(bitmap, 0, 0, null);
Paint deafalutPaint = new Paint();
canvas.drawRect(0, h, w, h + reflectionGapLine, deafalutPaint);
canvas.drawBitmap(reflectionImage, 0, h + reflectionGapLine, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
bitmapWithReflection.getHeight() + reflectionGapLine, 0x70ffffff,
0x00ffffff, Shader.TileMode.CLAMP);
paint.setShader(shader);
// Set the Transfer mode to be porter duff and destination in
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
// Draw a rectangle using the paint with our linear gradient
canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()
+ reflectionGapLine, paint);
return bitmapWithReflection;
}
// 8. bitmap ---Drawable
public static Drawable BitmapToDrawable(Bitmap bitmap, Context context) {
BitmapDrawable drawbale = new BitmapDrawable(context.getResources(),
bitmap);
return drawbale;
}
// 9. drawable进行缩放 ---> bitmap 然后比对bitmap进行缩放
public static Drawable ZoomDrawable(Drawable drawable, int w, int h) {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
// 调用5 中 drawable转换成bitmap
Bitmap oldbmp = DrawableToBitmap(drawable);
// 创建操作图片用的Matrix对象
Matrix matrix = new Matrix();
// 计算缩放比例
float sx = ((float) w / width);
float sy = ((float) h / height);
// 设置缩放比例
matrix.postScale(sx, sy);
// 建立新的bitmap,其内容是对原bitmap的缩放后的图
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
matrix, true);
return new BitmapDrawable(newbmp);
}
}
至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!
ImageView 使用详解的更多相关文章
- Button、ImageButton及ImageView详解
Button.ImageButton及ImageView详解 在应用程序开发过程中,很多时候需要将View的background或者src属性设置为图片,即美观又支持点击等操作.常见的有Button. ...
- Xamarin.Android通知详解
一.发送通知的机制 在日常的app应用中经常需要使用通知,因为服务.广播后台活动如果有事件需要通知用户,则需要通过通知栏显示,而在Xamarin.Android下的通知需要获取Notification ...
- Android开发重点难点1:RelativeLayout(相对布局)详解
前言 啦啦啦~博主又推出了一个新的系列啦~ 之前的Android开发系列主要以完成实验的过程为主,经常会综合许多知识来写,所以难免会有知识点的交杂,给人一种混乱的感觉. 所以博主推出“重点难点”系列, ...
- Dialog详解(包括进度条、PopupWindow、自定义view、自定义样式的对话框)
Dialog详解(包括进度条.PopupWindow.自定义view.自定义样式的对话框) Android中提供了多种对话框,在实际应用中我们可能会需要修改这些已有的对话框.本实例就是从实际出发, ...
- Uiautomator ——API详解(转载http://www.cnblogs.com/by-dream/p/4921701.html)
转载来源: 简单的例子 以一个简单的例子开始吧.我们完成一个 " 打开QQ,进入QQ空间,然后退出 " 的case. 代码如下: package QQ; import java.i ...
- Android Design Support Library使用详解
Android Design Support Library使用详解 Google在2015的IO大会上,给我们带来了更加详细的Material Design设计规范,同时,也给我们带来了全新的And ...
- iPhone应用开发 UITableView学习点滴详解
iPhone应用开发 UITableView学习点滴详解是本文要介绍的内容,内容不多,主要是以代码实现UITableView的学习点滴,我们来看内容. -.建立 UITableView DataTab ...
- TableLayout表格布局详解
一.Tablelayout简介 Tablelayout类以行和列的形式对控件进行管理,每一行为一个TableRow对象,或一个View控件.当为TableRow对象时,可在TableRow下添加子控件 ...
- IOS 手势详解
在IOS中手势可以让用户有很好的体验,因此我们有必要去了解一下手势. (在设置手势是有很多值得注意的地方) *是需要设置为Yes的点击无法响应* *要把手势添加到所需点击的View,否则无法响应* 手 ...
随机推荐
- CSS3 - vue中纯css实现柱状图表效果
背景 以前我们制作柱状图都用echarts或者其他同类型的图表插件 这次是个移动端的需求,而且这个图表需要动画 使用echarts就会显得过重,而且动画达不到我想要的效果(主要是我自己愚蠢想不到好的动 ...
- JavaScript面向对象之对象的声明、遍历和存储
一.对象的声明方式 1. 字面式(json格式)声明对象 var obj={ 属性名:属性值, 方法名:function(){ //函数执行体 } } 2. new 操作符+Object 声明对象 v ...
- Codeforces 755B:PolandBall and Game(map+思维)
http://codeforces.com/problemset/problem/755/B 题意:A可以喊出n个字符串,B可以喊出m个字符串,如果一个字符串之前被喊过,那么它再也不能喊了,A先喊,最 ...
- cocopods新建或者更新远端库主要操作步骤
1.搭建远程仓库(私有或者公有项目): 2.使用sourceTree拉去远程仓库: 3.打开拉去的项目仓库Finder,构建pod lib项目:pod lib create AFNetworking( ...
- os.path.join路径拼接的问题
问题一: import os a = os.path.join("/test1", "/test2") print(a) b = os.path.join(&q ...
- 寻觅Azure上的Athena和BigQuery(一):落寞的ADLA
AWS Athena和Google BigQuery都是亚马逊和谷歌各自云上的优秀产品,有着相当高的用户口碑.它们都属于无服务器交互式查询类型的服务,能够直接对位于云存储中的数据进行访问和查询,免去了 ...
- CI工具Jenkins的安装配置【linux】——jenkins集成sonarqube-异常解决
Setup 官网https://jenkins.io/ 下载war包,扔到tomcat下启动即可. 如果有port限制,在iptables中打开商品限制. 访问http://ip:port/jenki ...
- http接口测试和使用,首先要了解什么是http请求
http接口测试和使用,首先要了解什么是http请求: http请求通俗讲就是把客户端的东西通过http协议发送到服务端,服务端根据http协议的定义解析客户端发过 来的东西! http请求中常用到的 ...
- nginx实战操作(常用命令及配置)
1. nginx介绍 2. nginx常用命令 验证配置是否正确: nginx -t 查看Nginx的详细的版本号:nginx -V 查看Nginx的简洁版本号:nginx -v 启动Nginx:st ...
- ElementUI 源码简析——源码结构篇
ElementUI 作为当前运用的最广的 Vue PC 端组件库,很多 Vue 组件库的架构都是参照 ElementUI 做的.作为一个有梦想的前端(咸鱼),当然需要好好学习一番这套比较成熟的架构. ...