自定义View实现动态改变图片大小

一般用于下拉刷新时的动画效果。

image
/**
 * 动态缩放图片
 * Created by lzx on 2016/5/20.
 */
public class CustomPtrHeaderImage extends View {

    private Bitmap initialBitmap;
    private int measuredWidth;
    private int measuredHeight;
    private float mCurrentProgress;
    private Bitmap scaledBitmap;

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

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

    private void init() {
        //要缩放的图片
        initialBitmap = Bitmap.createBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_jing_gray_10));
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);

        measuredWidth = initialBitmap.getWidth();
        measuredHeight = initialBitmap.getHeight();

        if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(initialBitmap.getWidth(), initialBitmap.getHeight());
        } else if (widthSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(initialBitmap.getWidth(), heightSpecSize);
        } else if (heightSpecMode == MeasureSpec.AT_MOST) {
            setMeasuredDimension(widthSpecSize, initialBitmap.getHeight());
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        measuredWidth = getMeasuredWidth();
        measuredHeight = getMeasuredHeight();
        //根据第二阶段娃娃宽高  给椭圆形图片进行等比例的缩放
        scaledBitmap = Bitmap.createScaledBitmap(
                initialBitmap,
                measuredWidth * initialBitmap.getHeight() / initialBitmap.getWidth(),
                measuredWidth * initialBitmap.getHeight() / initialBitmap.getWidth(),
                true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //这个方法是对画布进行缩放,从而达到椭圆形图片的缩放,第一个参数为宽度缩放比例,第二个参数为高度缩放比例,
        canvas.scale(mCurrentProgress, mCurrentProgress, measuredWidth / 2, measuredHeight / 2);
        //将等比例缩放后的椭圆形画在画布上面
        canvas.drawBitmap(scaledBitmap, 0, measuredHeight / 4, null);
    }

    /**
     * 设置缩放比例,从0到1  0为最小 1为最大
     *
     * @param currentProgress
     */
    public void setCurrentProgress(float currentProgress) {
        mCurrentProgress = currentProgress;
    }
}

使用例子:
xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <SeekBar
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <com.lx.retrofitpar.CustomPtrHeaderImage
        android:id="@+id/customPtrHeaderImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp" />
</LinearLayout>

activity:

public class HeadViewActivity extends AppCompatActivity {

    private SeekBar seekBar;
    private CustomPtrHeaderImage mFirstView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_head_view);
        seekBar = (SeekBar) findViewById(R.id.seekbar);
        mFirstView = (CustomPtrHeaderImage) findViewById(R.id.customPtrHeaderImage);
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //计算出当前seekBar滑动的比例结果为0到1
                float currentProgress = (float) progress / (float) seekBar.getMax();
                //给我们的view设置当前进度值
                mFirstView.setCurrentProgress(currentProgress);
                //重画
                mFirstView.postInvalidate();
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,699评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,261评论 4 61
  • 褪去夏日的酷热,迎来朗朗金秋,万物清明让人心旷神怡,这就是传说中的“秋高气爽”了呀!然而,很多人的护肤字典里,“秋...
    神资讯阅读 269评论 0 0
  • 站在峰顶 淡瞥峰底 心如止水 何所牵挂 静立天黑 晚风拂面 雾霭深处 飘如秋叶 向死而生 辗转轮回 看破红尘 一念...
    蓝绿小巨人阅读 507评论 43 27
  • 从这周开始,我将开始学习ReactNative开发,对于有iOS开发基础的我来说,学习这门语言还是多多少少有些帮助...
    喵洛阅读 240评论 1 1