1. 程式人生 > >listview的條目上的圖片按照寬高比例進行縮放設定

listview的條目上的圖片按照寬高比例進行縮放設定

 
package com.itheima.googleplay2.view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
public class SubjectImageView extends ImageView {
    public SubjectImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 //獲取真實的圖片
        Drawable drawable = getDrawable();
        
        if(drawable!=null){
            //獲取真實的寬
            int width = drawable.getMinimumWidth();
            //獲取真實的高 
            int height = drawable.getMinimumHeight();
            //計算寬和高的比例 
            float scale = (float)width/height;
            //獲取測量寬的規則
            int withsize = MeasureSpec.getSize(widthMeasureSpec);
            //按照比例計算高的測量規則 
            int heightsize = (int) (withsize/scale);
            //設定高的測量規則 第一個值是按照比例計算的高 第二個引數是測量模式 精確
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightsize, MeasureSpec.EXACTLY);
            
        }
        
        
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }