技术标签: android
极力推荐文章:欢迎收藏
Android 干货分享
本篇文章主要介绍 Android
开发中的部分知识点,通过阅读本篇文章,您将收获以下内容:
一、ImageView 的继承关系
二、ImageView 常用方法
三、ImageView 背景 间距属性设置
四、使用Bitmap 类型动态设置ImageView 资源
五、ImageView 图片倒影实现
六、ImageView 图片缩放实现
七、ImageView 圆角图片实现
八、Bitmap 与Drawable 转换工具类
ImageView
的继承关系 如下:
java.lang.Object
↳ android.view.View
↳ android.widget.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>
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" />
padding background 属性设置
xml
使用ImageView
控件<ImageView
android:id="@+id/img_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp" />
java
类实现 // 1.从资源中获取Bitmap
ImageView mImageView1 = (ImageView) findViewById(R.id.img_1);
DrawableUtils.UseBitmap(this, mImageView1, R.drawable.gril);
DrawableUtils
类方法实现
// 1.从资源中获取Bitmap
public static void UseBitmap(Context context, ImageView imageView, int drawableId) {
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),
drawableId);
imageView.setImageBitmap(bitmap);
}
bitmap 类型的图片
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" />
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);
}
}
长按识别二维码,领福利
至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!
如有侵权,请联系小编,小编对此深感抱歉,届时小编会删除文章,立即停止侵权行为,请您多多包涵。
小礼物走一走,来简书关注我
文章浏览阅读3.7k次,点赞2次,收藏7次。最近摸索了一下ICC2创建tech file的过程。首先,我手上有什么?没错,我什么都没有,只有stdcell的Layout.1. 通过icfb dump出tech.lef文件。这个过程中,需要选择一个technology library, 而technolgy library可以选择项目中的tech lib。一般在创建tech lib的时候,会关联virtuoso的Tech file.因此,选择这个library,就相当于关联了一个tech file (virtuoso版本的)。然后export l_icc 没有.tf文件怎么办
文章浏览阅读1.3w次。react Native 执行 run-android 错误,报错如图报错信息:未能启动模拟器。原因:模拟器在启动前退出。原因:x86镜像的模拟器启动不了,因为HAMX没有安装。Android SDK已经集成了HAMX这个软件,我们需要做的就是找到他,然后安装就可以了。文件路径:存放于你的SDK下面的D:\Program Files\Android\AndroidSDK\(自己指定的SD..._error failed to launch emulator. reason: emulator exited before boot..
文章浏览阅读5.7k次,点赞12次,收藏89次。机械臂moveit编程(python)机械臂在笛卡尔空间的运动只能走点到点的直线运动,通过将位姿点加入waypoints中,机械臂会一次按照waypoints中的唯一依次沿直线运动到下一个点。程序流程:1.初始化需要控制的规划组;2.设置运动约束(可选);3.设置终端link;4.设置坐标系;5.建立一个waypoints空列表,将所需要到达的位姿加入waypoints列表;6.用..._omputecartesianpath
文章浏览阅读1.4k次,点赞3次,收藏3次。引子: ONVIF协议是监控领域的国际标准,现在基本所有的大厂IPC摄像头都支持此协议。如果想开发一套录播系统,支持IPC摄像头的集中录制,点播回放,以及实时监控流调度,提供的点播协议需要支持rtsp和HLS,可以利用的开源技术有哪些呢?本文提供的基本解决方案如下IPC 1------>IPC 2 ------> 基于onvif + live555 + ffmpeg 的Re..._录播服务器技术创新
文章浏览阅读716次。tf.nn.sparse_softmax_cross_entropy_with_logitslabel:不含独热编码,shape:[batch_size, ]logits:原始预测概率分布向量,shape:[batch_size, num_classes]logits = np.array([[0.3, 0.4, 0.3], [0.5, 0.2, 0.3], [0.1, 0.8, 0.1]]..._tensorflow二分类交叉熵损失
文章浏览阅读3.4k次,点赞2次,收藏30次。目录 什么是“3次握手,4次挥手” TCP服务模型 TCP头部 状态转换 为什么要“三次握手,四次挥手” 三次握手 四次挥手 “三次握手,四次挥手”怎么完成? 三次握手 ..._三次捂手
文章浏览阅读1.2k次。当我一开始在做全屏功能的时候,遇到了以下这个问题:Typescript error :Property mozRequestFullScreen' does not exist on type 'HTMLElement'.其他类似问题:property ‘xxx’ does not exist on type ‘yyy’解决:声明用let de : any;..._属性“mozrequestfullscreen”在类型“htmlelement”上不存在。你是否指的是“req
文章浏览阅读1k次。题目来源:URAL 1684. Jack's Last Word题意:输入a b 把b分成若干段 每一段都是a的前缀思路:b为主串 然后用a匹配b 记录到b的i位置最大匹配的长度 然后分割 分割的时候要从后往前如果a = abac b = abab 那么如果从前往后 首先覆盖了aba 然后b就不能覆盖了 从后往前就可以了 首先覆盖ab 下一次还是ab因为已经记录了到i位置的最大匹配_ural kmp
文章浏览阅读4.6k次,点赞3次,收藏34次。第三章 阈值抠图阈值处理方法直观简单,是一种基本的图像分割方法。根据图像的整体或部分信息适当选择阈值,像素值高于阈值时设为1/255,低于阈值时设为0,以此将图像中感兴趣的区域筛选出来生成掩模,再与原图像合成即可得到抠图图像。由于待处理图像的灰度级数与其灰度直方图是不确定的,对于不同的图像、不同的目标前景,需要选取适当的阈值处理方法进行图像分割。本章采用固定阈值、自适应阈值与色彩范围三种方法进行图像抠图处理。_opencv 扣图
文章浏览阅读3.1k次。广州子锐机器人技术有限公司:KUKA机器人出错提示和故障排除信息,提示窗口将显示各种类型的显示。它们既可以是不必确认的信息,也可以是必须予以确认的提示 一个提示可以由下列部分构成:1.1 提示组说明性提示例如按下某个不允许的键,它给使用者一个说明。状态提示提示设备的状态。该状态致使控制器发生反应(例如紧急关断等)消除提示的起因后,提示将被删除。安全起见,有时会设置一个有待确认的后续提示。确认性提..._kuka充电电池或电池保险丝损坏 — 无法缓存
文章浏览阅读2.6k次。最近在做项目中发现有地方计算出很多日期中的最大值,在这里找到了相关的思路。Collections.max(List<Date>)具体使用参考如下: List<Date> dateList = Lists.newArrayList(); for (PresentOccupationInfo item : list){ dateList.add(item.getPresentOccupationTime()); } _java set 排序取最大值
文章浏览阅读2.3w次,点赞6次,收藏56次。1. Testbench里面加入:initial begin $fsdbDumpfile("hello.fsdb"); $fsdbDumpvars("+all");end其中如果不用+all的参数,SV中的struct结构体无法被Dump出来。关于$fsdbDumpvars()以及其他相关FSDB Dumping Commands的参数的细节请参考Verdi安装目录的文档:li..._$fsdbdumpfile