自定義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,540評論 25 708
  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,254評論 4 61
  • 褪去夏日的酷熱,迎來朗朗金秋,萬物清明讓人心曠神怡,這就是傳說中的“秋高氣爽”了呀!然而,很多人的護膚字典里,“秋...
    神資訊閱讀 269評論 0 0
  • 站在峰頂 淡瞥峰底 心如止水 何所牽掛 靜立天黑 晚風拂面 霧靄深處 飄如秋葉 向死而生 輾轉輪回 看破紅塵 一念...
    藍綠小巨人閱讀 502評論 43 27
  • 從這周開始,我將開始學習ReactNative開發,對于有iOS開發基礎的我來說,學習這門語言還是多多少少有些幫助...
    喵洛閱讀 239評論 1 1