国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

自定義控件:等比例顯示控件RatioLayout

2019-11-07 23:01:31
字體:
來源:轉載
供稿:網友

圖1和圖2都是寬高比例相等,但是分辨率大小不一樣的圖片,應該按照比例顯示,使用等比例顯示控件后,圖2的顯示效果如圖3所示,和圖1的顯示效果是一致的,解決了寬高比相等或接近但分辨率大小不一樣而造成的顯示效果不一致的問題

package com.google.widget.view;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.widget.FrameLayout;import com.google.widget.R;/** * ============================================================ * Copyright:Google有限公司版權所有 (c) 2017 * Author: AllenIverson * Email: 815712739@QQ.com * GitHub: https://github.com/JackChen1999 * 博客: http://blog.csdn.net/axi295309066 * 微博: AndroidDeveloper * <p> * PRoject_Name:Widgets * Package_Name:com.google.widget * Version:1.0 * time:2016/2/15 14:09 * des : * gitVersion:$Rev$ * updateAuthor:$Author$ * updateDate:$Date$ * updateDes:${TODO} * ============================================================ **/public class RatioLayout extends FrameLayout { private float mPicRatio;//圖片的寬高比 public static final int RELATIVE_WIDTH = 0;//控件的寬度固定,根據比例求出高度 public static final int RELATIVE_HEIGHT = 1;//控件的高度固定,根據比例求出寬度 private int mRelative = RELATIVE_WIDTH; public RatioLayout(Context context) { this(context, null); } public RatioLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RatioLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs); } private void initView(Context context, AttributeSet attrs) { TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout); for (int i = 0; i < array.getIndexCount(); i++) { switch (i) { case R.styleable.RatioLayout_ratio: mPicRatio = array.getFloat(i, 2.43f); break; case R.styleable.RatioLayout_relative: mRelative = array.getInt(i, 0); break; } } array.recycle(); } public void setPicRatio(float picRatio) { mPicRatio = picRatio; } public void setRelative(int relative) { mRelative = relative; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); int childWidth = widthSize - getPaddingLeft() - getPaddingRight(); int childHeight = heightSize - getPaddingBottom() - getPaddingTop(); if (widthMode == MeasureSpec.EXACTLY && mPicRatio != 0 && mRelative == RELATIVE_WIDTH) { //修正高度的值 childHeight = (int) (childWidth / mPicRatio + 0.5f); } else if (heightMode == MeasureSpec.EXACTLY && mPicRatio != 0 && mRelative == RELATIVE_HEIGHT) { //修正寬度的值 childWidth = (int) (childHeight * mPicRatio + 0.5f); } int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY); int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY); measureChildren(childWidthMeasureSpec, childHeightMeasureSpec); setMeasuredDimension(childWidth + getPaddingLeft() + getPaddingRight(), childHeight + getPaddingBottom() + getPaddingTop()); /* widthMeasureSpec = MeasureSpec.makeMeasureSpec(childWidth + getPaddingLeft() + getPaddingRight(), MeasureSpec.EXACTLY); heightMeasureSpec = MeasureSpec.makeMeasureSpec(childHeight + getPaddingBottom() + getPaddingTop(), MeasureSpec.EXACTLY); super.measure(widthMeasureSpec, heightMeasureSpec);*/ }}<?xml version="1.0" encoding="utf-8"?><ScrollView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:google="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="分辨率:444x183,寬高比2.426" android:textSize="20sp"/> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="15dp" android:background="#4887EE" android:scaleType="fitXY" android:src="@m
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 休宁县| 辛集市| 海原县| 南昌县| 新津县| 安顺市| 北碚区| 自贡市| 石棉县| 乌拉特后旗| 巴塘县| SHOW| 获嘉县| 东安县| 青海省| 台南县| 红桥区| 临沭县| 宿松县| 武宣县| 曲松县| 凭祥市| 班玛县| 稻城县| 浮梁县| 平定县| 汾阳市| 家居| 双峰县| 沽源县| 克什克腾旗| 噶尔县| 九寨沟县| 新泰市| 廉江市| 垫江县| 靖州| 凤台县| 罗源县| 从江县| 呼玛县|