Android实现固定比例的ImageView
来源:互联网 发布:商业数据分析与挖掘 编辑:程序博客网 时间:2024/05/22 10:30
自定义imageview,其中的ratio属性代表宽是高的几倍,在xml中用:
app:ratio="1.5"
表示该图片宽是高的1.5倍
public class RatioImageView extends ImageView { /** * 宽高比例 */ private float mRatio = 0f; public RatioImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public RatioImageView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RatioImageView); mRatio = typedArray.getFloat(R.styleable.RatioImageView_ratio, 0f); typedArray.recycle(); } public RatioImageView(Context context) { super(context); } /** * 设置ImageView的宽高比 * * @param ratio */ public void setRatio(float ratio) { mRatio = ratio; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); if (mRatio != 0) { float height = width / mRatio; heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) height, MeasureSpec.EXACTLY); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Drawable drawable = getDrawable(); if (drawable != null) { drawable.mutate().setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY); } break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: Drawable drawableUp = getDrawable(); if (drawableUp != null) { drawableUp.mutate().clearColorFilter(); } break; } return super.onTouchEvent(event); }}
在styles.xml中加入以下代码
<declare-styleable name="RatioImageView"> <attr name="ratio" format="float"/> </declare-styleable>
阅读全文
0 0
- Android实现固定比例的ImageView
- 宽度固定高度按原图比例适应的ImageView
- Android ImageView显示比例
- ImageView 按固定宽度等比例缩放
- PercentLayout的固定比例
- ImageView的属性android:scaleType设置最佳比例图片显示
- ImageView的属性android:scaleType设置最佳比例图片显示
- ImageView的属性android:scaleType设置最佳比例图片显示
- Android开发教程--设置ImageView图片的显示比例
- Android Imageview控制它的宽高比例属性详解
- Android 实现TextView后面跟随一个高度和宽度固定的ImageView
- android创建固定宽度,高度的图片,比例缩放
- imageview如何固定宽度(例如都是320),而高度随真实图片的比例来设定
- [CAQ4]ImageView宽度固定,高度自适应的实现方式
- android LinearLayout android:layout_weight 作用,固定比例
- 用CSS实现高度宽度固定比例,宽度不固定
- 等比例宽高的ImageView
- 可以设置宽高比,宽度确定,高度比例缩放的ImageView快速实现
- 进程和线程的区别与联系
- 剑指offer--面试题20:表示数值的字符串
- 关于juqery对html的lable标签取值、赋值问题
- TeamTalk源码分析之win-client
- poj 3253 Fence Repair(优先队列)
- Android实现固定比例的ImageView
- 微信支付开发demo文件是干什么的简单说明
- KMP算法
- 数据挖掘RapidMiner工具使用----决策树案例分析
- 【贪心】Communication System POJ 1018
- 认识Docker(vmware+centos7+docker)
- Java之for循环优化方式-
- InputStream与OutputStream的使用例子
- liuxn防火墙