FitWidth ImageView和TopCrop ImageView
来源:互联网 发布:网易大数据平台 编辑:程序博客网 时间:2024/06/11 16:22
FitWidth ImageView: 宽度自适应
<com.kk.drama.view.widget.FitWidthImageView android:id="@+id/show_images" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/default_picture" />
public class FitWidthImageView extends ImageView{ public FitWidthImageView(Context context) { super(context); setup(); } public FitWidthImageView(Context context, AttributeSet attrs) { super(context, attrs); setup(); } public FitWidthImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setup(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = width * getDrawable().getIntrinsicHeight() / getDrawable().getIntrinsicWidth(); setMeasuredDimension(width, height); } private void setup() { setScaleType(ScaleType.CENTER_CROP); }}
TopCrop ImageView : 从头部Crop而不是center
<com.kk.drama.view.widget.FitWidthImageView android:id="@+id/show_images" android:layout_width="match_parent" android:layout_height="227dp" android:src="@drawable/default_picture" />
自己改名
public class FitWidthImageView extends ImageView{ public FitWidthImageView(Context context) { super(context); setup(); } public FitWidthImageView(Context context, AttributeSet attrs) { super(context, attrs); setup(); } public FitWidthImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); setup(); } private void setup() { setScaleType(ScaleType.CENTER_CROP); setScaleType(ScaleType.MATRIX); } @Override protected boolean setFrame(int frameLeft, int frameTop, int frameRight, int frameBottom) { float frameWidth = frameRight - frameLeft; float frameHeight = frameBottom - frameTop; float originalImageWidth = (float) getDrawable().getIntrinsicWidth(); float originalImageHeight = (float) getDrawable().getIntrinsicHeight(); float usedScaleFactor = 1; if ((frameWidth > originalImageWidth) || (frameHeight > originalImageHeight)) { // If frame is bigger than image // => Crop it, keep aspect ratio and position it at the bottom and center horizontally float fitHorizontallyScaleFactor = frameWidth / originalImageWidth; float fitVerticallyScaleFactor = frameHeight / originalImageHeight; usedScaleFactor = Math.max(fitHorizontallyScaleFactor, fitVerticallyScaleFactor); } float newImageWidth = originalImageWidth * usedScaleFactor; float newImageHeight = originalImageHeight * usedScaleFactor; Matrix matrix = getImageMatrix(); matrix.setScale(usedScaleFactor, usedScaleFactor, 0, 0); // Replaces the old matrix completly // matrix.postTranslate((frameWidth - newImageWidth) / 2, frameHeight - newImageHeight);//BottomCrop matrix.postTranslate((frameWidth - newImageWidth) / 2, 0);//Top Crop setImageMatrix(matrix); return super.setFrame(frameLeft, frameTop, frameRight, frameBottom); }}
0 0
- FitWidth ImageView和TopCrop ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- imageView
- imageView
- ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- ImageView
- imageView
- ImageView
- MapReduce错误处理,任务调度及Shuffle过程
- JAVA & JSP 之旋转图片(JAVA图形化小程序——>网页应用)
- 九度OJ-题目1372:最大子向量和(连续子数组的最大和)
- 第七章
- 学习使用第三方控件IECapt截取网页图片
- FitWidth ImageView和TopCrop ImageView
- 写日志文件的代码
- replace into 浅析之一
- Sqlserver 查找存储过程,函数中是否存在某字段
- 利用scrapy抓取网易新闻并将其存储在mongoDB
- Maven入门指南⑤:使用Nexus搭建Maven私服
- 数据库触发器的经验总结
- Device Tree
- 利用参数传参实现计算器的设计