用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - android代码库

缩放的ImageView

2015-02-25 作者: 云代码会员举报

[android]代码库

package com.yx.straightline.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class ScaleImageView extends ImageView {
    private ImageChangeListener imageChangeListener ;
    private boolean scaleToWidth = false; // this flag determines if should measure height manually dependent of width

    public ScaleImageView(Context context) {
           super(context);
          init();
   }

    public ScaleImageView(Context context, AttributeSet attrs, int defStyle) {
           super(context, attrs, defStyle);
          init();
   }

    public ScaleImageView(Context context, AttributeSet attrs) {
           super(context, attrs);
          init();
   }

    private void init(){
           this.setScaleType(ScaleType.CENTER_INSIDE);
   }

    @Override
    public void setImageBitmap(Bitmap bm) {
           super.setImageBitmap(bm);
           if (imageChangeListener != null)
                  imageChangeListener.changed((bm == null));
   }

    @Override
    public void setImageDrawable(Drawable d) {
           super.setImageDrawable(d);
           if (imageChangeListener != null)
                  imageChangeListener.changed((d == null));
   }

    @Override
    public void setImageResource(int id){
           super.setImageResource(id);
   }

    public interface ImageChangeListener {
           // a callback for when a change has been made to this imageView
           void changed(boolean isEmpty);
   }

    public ImageChangeListener getImageChangeListener() {
           return imageChangeListener ;
   }

    public void setImageChangeListener(ImageChangeListener imageChangeListener) {
           this.imageChangeListener = imageChangeListener;
   }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
          
           int widthMode = MeasureSpec.getMode(widthMeasureSpec);
           int heightMode = MeasureSpec.getMode(heightMeasureSpec);
           int width = MeasureSpec.getSize(widthMeasureSpec);
           int height = MeasureSpec.getSize(heightMeasureSpec);
          
           /**
           * if both width and height are set scale width first. modify in future if necessary
           */
          
           if(widthMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.AT_MOST ){
                  scaleToWidth = true ;
          } else if (heightMode == MeasureSpec.EXACTLY || heightMode == MeasureSpec.AT_MOST ){
                  scaleToWidth = false ;
          } else throw new IllegalStateException("width or height needs to be set to match_parent or a specific dimension");
          
           if(getDrawable()==null || getDrawable().getIntrinsicWidth()==0 ){
                  // nothing to measure
                  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
                  return;
          } else{
                  if(scaleToWidth ){
                        int iw = this .getDrawable().getIntrinsicWidth();
                        int ih = this .getDrawable().getIntrinsicHeight();
                        int heightC = width*ih/iw;
                        if(height > 0)
                        if(heightC>height){
                               // dont let hegiht be greater then set max
                              heightC = height;
                              width = heightC*iw/ih;
                       }
                       
                        this.setScaleType(ScaleType.CENTER_CROP);
                       setMeasuredDimension(width, heightC);
                       
                 } else{
                        // need to scale to height instead
                        int marg = 0;
                        if(getParent()!=null){
                               if(getParent().getParent()!=null){
                                     marg+= ((RelativeLayout) getParent().getParent()).getPaddingTop();
                                     marg+= ((RelativeLayout) getParent().getParent()).getPaddingBottom();
                              }
                       }
                       
                        int iw = this .getDrawable().getIntrinsicWidth();
                        int ih = this .getDrawable().getIntrinsicHeight();

                       width = height*iw/ih;
                       height-=marg;
                       setMeasuredDimension(width, height);
                 }

          }
   }

}

知识点:
MeasureSpec.EXACTLY
MeasureSpec.AT_MOST
MeasureSpec.UNSPECIFIED
MeasureSpec.getMode方法和MeasureSpec.getSize方法


网友评论    (发表评论)

共4 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...