一般用于下拉刷新时的动画效果。
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) {
}
});
}
}
网友评论