一般用于下拉刷新時(shí)的動(dòng)畫(huà)效果获诈。
image
/**
* 動(dòng)態(tài)縮放圖片
* 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();
//根據(jù)第二階段娃娃寬高 給橢圓形圖片進(jìn)行等比例的縮放
scaledBitmap = Bitmap.createScaledBitmap(
initialBitmap,
measuredWidth * initialBitmap.getHeight() / initialBitmap.getWidth(),
measuredWidth * initialBitmap.getHeight() / initialBitmap.getWidth(),
true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//這個(gè)方法是對(duì)畫(huà)布進(jìn)行縮放,從而達(dá)到橢圓形圖片的縮放七扰,第一個(gè)參數(shù)為寬度縮放比例五辽,第二個(gè)參數(shù)為高度縮放比例,
canvas.scale(mCurrentProgress, mCurrentProgress, measuredWidth / 2, measuredHeight / 2);
//將等比例縮放后的橢圓形畫(huà)在畫(huà)布上面
canvas.drawBitmap(scaledBitmap, 0, measuredHeight / 4, null);
}
/**
* 設(shè)置縮放比例,從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) {
//計(jì)算出當(dāng)前seekBar滑動(dòng)的比例結(jié)果為0到1
float currentProgress = (float) progress / (float) seekBar.getMax();
//給我們的view設(shè)置當(dāng)前進(jìn)度值
mFirstView.setCurrentProgress(currentProgress);
//重畫(huà)
mFirstView.postInvalidate();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
}
}