編輯:關於Android編程
利用自定義View的onDraw()方法,可以繪制很多種圖形,進度框只是其中之一。
這是一個模擬下載的demo。
demo1
public class FirstProgressView extends View{
private int width;
private int height;
private int progress;
private int maxProgress = 100;
private Paint mPaintBackGround;
private Paint mPaintCurrent;
private Paint mPaintText;
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
this.progress = progress;
invalidate();
}
public int getMaxProgress() {
return maxProgress;
}
public void setMaxProgress(int maxProgress) {
this.maxProgress = maxProgress;
}
public FirstProgressView(Context context) {
super(context);
}
public FirstProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintBackGround = new Paint();//新定義一個畫筆,用來畫背景
mPaintBackGround.setColor(Color.GRAY);//設置畫筆顏色
mPaintBackGround.setAntiAlias(true);//設置為true,代表抗鋸齒
mPaintCurrent = new Paint();//用於畫進度圖
mPaintCurrent.setColor(Color.GREEN);
mPaintCurrent.setAntiAlias(true);
mPaintText = new Paint();//用來畫文本的畫筆
mPaintText.setColor(Color.BLACK);
mPaintText.setAntiAlias(true);
mPaintText.setTextAlign(Paint.Align.CENTER);//設置文本排布方式:正中央
mPaintText.setTextSize(50);//設置文本大小,這裡為50xp
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//左起:圓心x坐標,圓心y坐標,半徑,Paint對象(畫筆)
canvas.drawCircle(width / 2, height / 2, 400, mPaintBackGround);
canvas.drawCircle(width/2,height/2,progress*400f/maxProgress,mPaintCurrent);
//左起:文本內容,起始位置x坐標,起始位置y坐標,畫筆
canvas.drawText(progress*100f/maxProgress+%,width / 2, height / 2,mPaintText);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//該View布局寬
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
//該View布局高
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
//設定本View的大小的方法
setMeasuredDimension(width, height);
}
}
demo2
public class SecondProgressView extends View {
private int width;
private int height;
private int progress;
private int maxProgress = 100;
private Paint mPaintBackGround;
private Paint mPaintCurrent;
private Paint mPaintText;
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
this.progress = progress;
invalidate();
}
public int getMaxProgress() {
return maxProgress;
}
public void setMaxProgress(int maxProgress) {
this.maxProgress = maxProgress;
}
public SecondProgressView(Context context) {
super(context);
}
public SecondProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintBackGround = new Paint();
mPaintBackGround.setColor(Color.GRAY);
mPaintBackGround.setStyle(Paint.Style.STROKE);
mPaintBackGround.setStrokeWidth(20);
mPaintBackGround.setAntiAlias(true);
mPaintCurrent = new Paint();
mPaintCurrent.setColor(Color.BLUE);
mPaintCurrent.setAntiAlias(true);
mPaintText = new Paint();
mPaintText.setColor(Color.BLACK);
mPaintText.setAntiAlias(true);
mPaintText.setTextAlign(Paint.Align.CENTER);
mPaintText.setTextSize(50);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawRect(width / 2 - 200, height / 2 - 200, width / 2 + 200, height / 2 + 200, mPaintBackGround);
canvas.drawRect(width/2-190,height/2-190+(380-progress*380f/maxProgress),width/2+190,height/2+190,mPaintCurrent);
canvas.drawText(progress*100f/maxProgress+%,width / 2, height / 2,mPaintText);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
demo3:<喎?/kf/ware/vc/" target="_blank" class="keylink">vc3Ryb25nPjwvcD4NCjxwcmUgY2xhc3M9"brush:java;">
需要在布局文件中加載自定義View,這裡隨便加載一個作示范(自定義View都這麼加載): 主活動的中自定義View也需要聲明和findViewById,這裡寫的是一個模擬下載的小demo,加載的是第三種自定義View,其他的自定義View加載方式完全一樣。 public class ThirdProgressView extends View {
private int width;
private int height;
private int progress;
private int maxProgress = 100;
private Paint mPaintBackGround;
private Paint mPaintCurrent;
private Paint mPaintText;
public void setWidth(int width) {
this.width = width;
}
public void setHeight(int height) {
this.height = height;
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
this.progress = progress;
invalidate();
}
public int getMaxProgress() {
return maxProgress;
}
public void setMaxProgress(int maxProgress) {
this.maxProgress = maxProgress;
}
public ThirdProgressView(Context context) {
super(context);
}
public ThirdProgressView(Context context, AttributeSet attrs) {
super(context, attrs);
mPaintBackGround = new Paint();
mPaintBackGround.setColor(Color.GRAY);
mPaintBackGround.setStrokeWidth(60);
mPaintBackGround.setStyle(Paint.Style.STROKE);
mPaintBackGround.setAntiAlias(true);
mPaintCurrent = new Paint();
mPaintCurrent.setColor(Color.GREEN);
mPaintCurrent.setStrokeWidth(60);
mPaintCurrent.setStyle(Paint.Style.STROKE);
mPaintCurrent.setAntiAlias(true);
mPaintText = new Paint();
mPaintText.setColor(Color.BLACK);
mPaintText.setAntiAlias(true);
mPaintText.setTextAlign(Paint.Align.CENTER);
mPaintText.setTextSize(50);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(width / 2, height / 2, 400, mPaintBackGround);
RectF oval = new RectF(width/2-400,height/2-400,width/2+400,height/2+400);
//左起分別是RectF對象,起始角度,終止角度,是否顯示扇邊(如果為true畫出的是一個扇形),Paint對象
canvas.drawArc(oval,0,progress*360f/maxProgress,false,mPaintCurrent);
canvas.drawText(progress*100f/maxProgress+%,width / 2, height / 2,mPaintText);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
width = getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec);
height = getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec);
setMeasuredDimension(width, height);
}
}
public class MainActivity extends Activity {
private int progress;
private Button mButtonStart;
private ThirdProgressView mProgressView;
private static final int DOWNLOAD_UPDATE = 0x99;
//模擬下載
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//處理msg
switch (msg.what) {
case DOWNLOAD_UPDATE:
progress++;
if (progress <= 100) {
mProgressView.setProgress(progress);
sendEmptyMessageDelayed(DOWNLOAD_UPDATE, 100);//每隔100毫秒發送一次handler
}
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButtonStart = (Button) findViewById(R.id.button_start);
mProgressView = (ThirdProgressView) findViewById(R.id.progress_view_first);
mButtonStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//向handler發送一個延時空消息,1000毫秒後發送
mHandler.sendEmptyMessageDelayed(DOWNLOAD_UPDATE, 1000);
}
});
}
}
這篇博客我們來介紹一下建造者模式(Builder Pattern),建造者模式又被稱為生成器模式,是創造性模式之一,與工廠方法模式和抽象工廠模式不同,後兩者的目的是為了實
聲音的類型有:定義在AudioSystem.java文件中 /* The default audio stream */public static final
在程序開發過程中,LOG是廣泛使用的用來記錄程序執行過程的機制,它既可以用於程序調試,也可以用於產品運營中的事件記錄。在Android系統中,提供了簡單、便利的LOG機制
Android實戰打飛機游戲子彈生成,新建子彈類public class Bullet { // 子彈圖片資源 public Bitmap bmpBullet; // 子