前言
最近在項目中,開發下拉刷新列表時,要做一個動畫,下拉的時候慢慢填充一個動畫,做到一個滿血復活的效果(設計師的解釋,你懂得--),然後自己寫了一個自定義的動畫實現此效果。
代碼
package com.example.loading.myloading;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
/**
* 描述
* Created by fww
* date 16/4/25.
*/
public class LoadingView extends View {
private Bitmap bitmap;
private Paint paint1;
private Paint paint2;
private Paint paint3;
private Canvas canvas;
private int h;
public LoadingView(Context context) {
this(context, null);
}
public LoadingView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
paint1 = new Paint();
paint1.setColor(Color.RED);
paint2 = new Paint();
paint2.setColor(Color.GRAY);
paint3 = new Paint();
paint3.setColor(Color.WHITE);
Bitmap bitmap1 = BitmapFactory.decodeResource(context.getResources(), R.drawable.loading_0000);
bitmap = bitmap1.extractAlpha();// 獲取一個透明圖片
h = bitmap.getHeight();//初始化y軸座標
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int width;
int height ;
if (widthMode == MeasureSpec.EXACTLY)
{
width = widthSize;
} else
{
float textWidth = bitmap.getWidth();
int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight());
width = desired;
}
if (heightMode == MeasureSpec.EXACTLY)
{
height = heightSize;
} else
{
float textHeight = bitmap.getHeight();
int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom());
height = desired;
}
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas)
{
this.canvas = canvas;
canvas.drawColor(Color.WHITE); // 畫布顏色
canvas.drawBitmap(bitmap, 0, 0, paint2); //畫一個灰的圖片
canvas.save();
//按y來裁剪區域
canvas.clipRect(0, h +0, bitmap.getWidth()+0,
bitmap.getHeight()+0);
canvas.drawBitmap(bitmap, 0, 0, paint1);
canvas.restore();
}
public void updateView(int i){
h =i;
postInvalidate();
}
}
MainActivity
package com.example.loading.myloading;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private LinearLayout mView;
private LoadingView loadingView;
private Button mBtn;
private int height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mView = (LinearLayout) findViewById(R.id.view);
loadingView = (LoadingView) findViewById(R.id.loadingview);
mBtn = (Button) findViewById(R.id.but);
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.loading_0000);
height = bitmap.getHeight();
mBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
height= height-2;
if(height>0){
loadingView.updateView(height);
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
下面有開發的Demo 環境爲AndroidStudio