继承进度条代码:
インポートandroid.content.Context。
輸入android.graphics.Canvas。
輸入android.graphics.Color;
輸入android.graphics.Paint;
輸入android.graphics.Rect;
輸入android.util.AttributeSet;
輸入android.widget.ProgressBar;
パブリッククラスPecentProgressはプログレス{延びている
文字列のテキストを、
ペイントペイント;
公共PecentProgress(コンテキスト・コンテキスト){
スーパー(コンテキスト)。
// TODO自動生成されたコンストラクタスタブ
のSystem.out.println( "1");
initText();
}
公共PecentProgress(コンテキスト・コンテキスト、属性セットattrsに、INT defStyle){
スーパー(文脈、attrsに、defStyle)。
// TODO自動生成されたコンストラクタスタブ
のSystem.out.println( "2");
initText();
}
公共PecentProgress(コンテキスト・コンテキスト、属性セットのATTRS){
スーパー(文脈、ATTRS)。
// TODO自動生成されたコンストラクタスタブ
のSystem.out.println( "3");
initText();
}
@Override
公共同期ボイドでsetProgress(INT進行){
// TODO自動生成方法スタブ
のsetText(進捗状況)
super.setProgress(進行)。
}
@Override
保護同期ボイドonDraw(キャンバスキャンバス){
// TODO自動生成方法スタブ
super.onDraw(キャンバス)。
//this.setText()。
RECT RECT =新しいのRect();
this.Paint.getTextBounds(this.text、0、this.text.length()、RECT)。
INT X =(のgetWidth()/ 2) - rect.centerX()。
Y = INT(のgetHeight()/ 2) - rect.centerY()。
canvas.drawText(this.text、X、Y、this.Paint)。
}
//初始化
プライベートボイドinitText(){
this.Paint =新しいペイント()。
this.Paint.setColor(Color.BLUE)。
}
プライベートボイドのsetText(){
のsetText(this.getProgress())。
}
//设置文字内容
プライベートボイドのsetText(進捗をINT){
int型私は=(進行* 100)/this.getMaxを();
= String.valueOf This.Text(I)+ "%";
}
}
ページコード
<パッケージcom.example.utils.PecentProgress精度クラスが記事を継承場所の上
アンドロイド:ID = "@ + ID / load_proj_bar"
アンドロイド:最大= "100"
アンドロイド:layout_gravity = "センター"
のandroid:layout_width = "match_parent"
のandroid:layout_height = "match_parent"
アプリ:layout_heightPercent = "80%" //安卓百分比
アプリ:layout_widthPercent = "90%"
スタイル= "@アンドロイド:スタイル/ Widget.ProgressBar.Horizontal"
アンドロイド:可視= "可視"
/>
//初期化
プライベートPecentProgressの進捗= NULL;
プロ=(PecentProgress)findViewById(R.id.load_proj_bar)。
//、子スレッドを開き、プログレスバーを設定します
プロ.setMax(100)。
プロ.setProgress(0)。