• 13.按比例显示图片、自定义属性、测量


    有时候服务器返回的图片有可能宽高是不一样的,所以需要按照一定宽高比例去显示,修改专题界面

    自定义属性
    1. <resources>
    2. <declare-styleable name="com.itheima.googleplay.view.RatioLayout">
    3. <attr name="ratio" format="float"></attr>
    4. </declare-styleable>
    5. </resources>
    RatioLayout 
    1. public class RatioLayout extends FrameLayout {
    2. // 按照宽高比例去显示
    3. private float ratio = 2.43f; // 比例值,不要去写死,这样只需要调下方法根据自己需要去修改
    4. public void setRatio(float ratio) {
    5. this.ratio = ratio;
    6. }
    7. public RatioLayout(Context context) {
    8. super(context);
    9. }
    10. public RatioLayout(Context context, AttributeSet attrs, int defStyle) {
    11. super(context, attrs, defStyle);
    12. // 参数1 命名控件 参数2 属性的名字 参数3 默认的值
    13. float ratio = attrs.getAttributeFloatValue(
    14. "http://schemas.android.com/apk/res/com.itheima.googleplay",
    15. "ratio", 2.43f);
    16. setRatio(ratio);
    17. }
    18. public RatioLayout(Context context, AttributeSet attrs) {
    19. this(context, attrs, 0);
    20. }
    21. // 测量当前布局
    22. @Override
    23. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    24. // widthMeasureSpec 宽度的规则 包含了两部分 模式 值
    25. int widthMode = MeasureSpec.getMode(widthMeasureSpec); // 模式
    26. int widthSize = MeasureSpec.getSize(widthMeasureSpec);// 宽度大小
    27. int width = widthSize - getPaddingLeft() - getPaddingRight();// 去掉左右两边的padding
    28. int heightMode = MeasureSpec.getMode(heightMeasureSpec); // 模式
    29. int heightSize = MeasureSpec.getSize(heightMeasureSpec);// 高度大小
    30. int height = heightSize - getPaddingTop() - getPaddingBottom();// 去掉上下两边的padding
    31. if (widthMode == MeasureSpec.EXACTLY
    32. && heightMode != MeasureSpec.EXACTLY) {
    33. // 修正一下 高度的值 让高度=宽度/比例
    34. height = (int) (width / ratio + 0.5f); // 保证4舍五入
    35. } else if (widthMode != MeasureSpec.EXACTLY
    36. && heightMode == MeasureSpec.EXACTLY) {
    37. // 由于高度是精确的值 ,宽度随着高度的变化而变化
    38. width = (int) ((height * ratio) + 0.5f);
    39. }
    40. // 重新制作了新的规则
    41. widthMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY,
    42. width + getPaddingLeft() + getPaddingRight());
    43. heightMeasureSpec = MeasureSpec.makeMeasureSpec(MeasureSpec.EXACTLY,
    44. height + getPaddingTop() + getPaddingBottom());
    45. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    46. }
    47. }
    布局:把imageview包裹住就可以不用修改imageview了,它根据它的父容器去显示
    1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:itheima="http://schemas.android.com/apk/res/com.itheima.googleplay"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >
    2. <com.itheima.googleplay.view.RatioLayout
    3. android:id="@+id/rl_layout"
    4. android:layout_width="match_parent"
    5. android:layout_height="wrap_content"
    6. android:padding="5dp"
    7. itheima:ratio="2.43">
    8. <ImageView
    9. android:id="@+id/item_icon"
    10. android:layout_width="match_parent"
    11. android:layout_height="wrap_content"
    12. android:scaleType="fitCenter"
    13. android:src="@drawable/ic_default" />
    14. </com.itheima.googleplay.view.RatioLayout>

     




  • 相关阅读:
    用互不相同的fib数列的数分解任意整数。
    2015 初赛TG 错题解析
    【模板】判断二叉查找树
    【初赛】完善程序题解题技巧 && 近六年PJ完善程序真题解析
    [NOIP 2012普及组 No.2] 寻宝
    [NOIP 2012普及组 No.1] 质因数分解
    [NOIP 2013普及组 No.4] 车站分级
    [NOIP 2013普及组 No.3] 小朋友的数字
    [NOIP 2013普及组 No.2] 表达式求值
    [NOIP 2013普及组 No.1] 计数问题
  • 原文地址:https://www.cnblogs.com/sixrain/p/4979314.html
Copyright © 2020-2023  润新知