使用camera、matrix仿即刻APP換一換
阿新 • • 發佈:2019-02-08
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//畫當前圖片
camera.save();
camera.rotateX(0);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-width, 0);
matrix.postTranslate(width, offsetY);
canvas .drawBitmap(currentBitmap, matrix, null);
//畫下一張圖片
camera.save();
camera.rotateX(0);
camera.getMatrix(matrix);
camera.restore();
matrix.preTranslate(-width , -height);
matrix.postTranslate(width, offsetY);
canvas.drawBitmap(nextBitmap, matrix , null);
}
圖片縮放
private Bitmap scaleBitmap(Bitmap bitmap) {
int originHeight = bitmap.getHeight();
Log.d("originHeight", originHeight + "");
int originWidth = bitmap.getWidth();
float scaleWidth = ((float) width)/ originWidth;
float scaleHeight = ((float ) height)/ originHeight;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
return Bitmap.createBitmap(bitmap, 0, 0, originWidth, originHeight, matrix, false);
}
屬性動畫
public void changePic(Bitmap bitmap) {
currentBitmap = nextBitmap;
nextBitmap = scaleBitmap(bitmap);
ValueAnimator animator = ObjectAnimator.ofFloat(0, height);
animator.setDuration(1000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
offsetY = (float) animation.getAnimatedValue();
postInvalidate();
}
});
animator.start();
}