ImageView相关

package com.lx.aidlservice;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;
import android.widget.TextView;


public class ImageViewSec extends ImageView {


    private static final String TAG = "ImageViewSec";
    private TextView mTextView;
    private int mDrawableWidth;
    private int mDrawableHeight;
    
    private String mContent;
    
    public String getContent() {
        return mContent;
    }
    
    public void setContent(String content) {
        this.mContent = content;
    }
    
    public ImageViewSec(Context context) {
        super(context);
        initTextView(context);
    }
    public ImageViewSec(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }


    public ImageViewSec(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    
    private void initTextView(Context context) {
        if (null == mTextView) {
            mTextView = new TextView(context);
        }
    }
    
    @Override
    public void setImageDrawable(Drawable drawable) {
        super.setImageDrawable(drawable);
        if (null != drawable) {
            updateDrawable(drawable);
        }
    }
    
    private void updateDrawable(Drawable d) {
        if (d != null) {
            mDrawableWidth = d.getIntrinsicWidth();
            mDrawableHeight = d.getIntrinsicHeight();
            configureBounds();
        } else {
            mDrawableWidth = mDrawableHeight = -1;
        }
    
    }
    
    private void configureBounds() {
        if (null == getDrawable()) {
            return;
        }
        int dwidth = mDrawableWidth;
        int dheight = mDrawableHeight;


        int vwidth = getWidth() - getPaddingLeft() - getPaddingRight();
        int vheight = getHeight() - getPaddingTop() - getPaddingBottom();
        if (ScaleType.CENTER_CROP == getScaleType()) {
            float scale;
            float dx = 0, dy = 0;
            if (dwidth * vheight > vwidth * dheight) {
                scale = (float) vheight / (float) dheight; 
                dx = (vwidth - dwidth * scale) * 0.5f;
            } else {
                scale = (float) vwidth / (float) dwidth;
                dy = (vheight - dheight * scale) * 0.5f;
            }
            Log.d(TAG, "configureBounds scale=" + scale + " vwidth=" + vwidth + " vheight=" 
            + vheight + " dwidth=" + dwidth + " dheight=" + dheight);
            
        }
        
    }

}



ImageView.ScaleType|android:scaleType值的意义:

ImageView.ScaleType.CENTER|android:scaleType="center" 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分显示

ImageView.ScaleType.CENTER_CROP|android:scaleType="centerCrop按比例扩大图片的size居中显示,使得图片长 (宽)等于或大于View的长(宽)

ImageView.ScaleType.CENTER_INSIDE|android:scaleType="centerInside将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片长(宽)等于或小于View的长(宽)

ImageView.ScaleType.FIT_CENTER|android:scaleType="fitCenter把图片按比例扩大(缩小)到View的宽度,居中显示

ImageView.ScaleType.FIT_END|android:scaleType="fitEnd把图片按比例扩大(缩小)到View的宽度,显示在View的下部分位置

ImageView.ScaleType.FIT_START|android:scaleType="fitStart把图片按比例扩大(缩小)到View的宽度,显示在View的上部分位置

ImageView.ScaleType.FIT_XY|android:scaleType="fitXY把图片按照指定的大小在View中显示

ImageView.ScaleType.MATRIX|android:scaleType="matrix用matrix来绘制





looking for. 

Here is a table showing samples for all scale types for the ImageView.

scaleTypefill_parent x fill_parent200dp x 200dp200dp x 200dp
with Overlay
matrix
fitXY
fitStart
fitCenter
fitEnd
center
centerCrop
centerInside

The column labeled "fill_parent x fill_parent" represents the following layout:

 <?xml version="1.0" encoding="utf-8"?>  
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent">  
   <ImageView android:layout_width="fill_parent"  
               android:layout_height="fill_parent"  
               android:src="@drawable/eureka"  
               android:scaleType="matrix">  
   </ImageView>  
 </FrameLayout>  

The column labeled "200dp x 200dp" represents the following layout:
 <?xml version="1.0" encoding="utf-8"?>  
 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent">  
   <ImageView android:layout_width="200dp"  
               android:layout_height="200dp"  
               android:layout_gravity="center"  
               android:src="@drawable/eureka"  
               android:scaleType="matrix">  
   </ImageView>  
 </FrameLayout>  



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值