Androidの進捗状況を示す円形の進捗バー
29990 ワード
extens:http://blog.csdn.net/xiaanming/article/details/10298163
転載は住所を明記してください.http://blog.csdn.net/xiaanming/article/details/10298163
多くの場合、システムが持っているViewは私達の機能の需要を満たしてくれないので、自分でViewをカスタマイズして、Viewをカスタマイズします.私達は先にViewを継承して、種類の構造方法を追加して、父種類のViewを書き直します.時々、私達は自分の属性をカスタマイズしたいです.簡単な例を挙げて、プロジェクトの中の複数のインターフェースは自分のカスタマイズしたViewを使います.各インターフェイスはViewの色をカスタマイズするべきです.この時はカスタム属性がないと、違った色のViewを構築する必要がありますか?ですから、私たちは自分の属性をカスタマイズして、私たちの需要に応じてカスタマイズする必要があります.属性はvaluesの下でatrs.xmlファイルを作成します.その中で定義したい属性を定義して、そしてカスタムViewの中で相応の修正をします.私たちはやはり小さい例を使ってカスタムViewとカスタム属性の使用を見てみます.
今日はみんなを連れて、自分で進捗のある円形の進捗状況を定義します.効果を見てみましょう.
上から見ると、私たちは円環の色、円環の進捗度の色をカスタマイズできますが、進捗率、進捗率の色、進捗度が実心か中空かなどを表示しますか?次に私達は皆さんにどう定義するかを教えます.
1.valuesの下にatrs.xmlを新たに作成します.ここでは私たちの属性を定義します.異なる属性は異なるformatに対応しています.属性に対応するformatはhttp://blog.csdn.net/pgalxx/article/details/6766677を参照してもいいです.紹介したのはまだ詳細です.次に私はカスタムのこのプログレスバーで使用した属性を貼ります.
2.カスタムViewの属性は定義されています.次に属性とコードをどうやって取得して作成しますか?構造方法で私達が定義した関連属性を取得する必要があります.まずcontext.obtainStylodAttributes(atrs、R.style.RoundPrograte Bar)を呼び出してTypediaryを取得して、Tyrayなどの属性を指定します.
上のコードは、roundColor=mTypediary.get Color(R.style.RoundPrograss BarColor,Color.RED)のようなものです.get Color方法の最初のパラメータはXMLファイルで定義された色です.もし私達がカスタムしたViewに色を定義していないなら、彼は第二のパラメータのデフォルト値、つまりColor.REDを使います.
3.皆さんに分かりやすくするために、私はViewをカスタマイズしたコードを全部貼り付けます.中のコードは私も詳しい注釈があります.
4.上記のステップを通じて、私達はカスタムViewとカスタムViewの属性を実現しました.もちろん使う過程ではまだ少し変化があります.私達はインターフェースレイアウトの一番上に加えなければなりません.
これは名前空間です.
赤い部分はカスタム属性のプレフィックスです.どういう意味ですか?Androidシステムのコントロールについて、私達はそのコントロールの属性を定義します.android:XXX="XXXX"を使います.custom:XXX="XXXX" 緑色の部分は私達のカバンの名前です. です.
上の二つのステップで属性を定義できます.カスタムViewを貼ってXMLで使用します.
今日はこれで終わります.もしみなさんに何か質問がありましたら、メッセージをください.
プロジェクトのソースコードをクリックしてダウンロードします。
転載は住所を明記してください.http://blog.csdn.net/xiaanming/article/details/10298163
多くの場合、システムが持っているViewは私達の機能の需要を満たしてくれないので、自分でViewをカスタマイズして、Viewをカスタマイズします.私達は先にViewを継承して、種類の構造方法を追加して、父種類のViewを書き直します.時々、私達は自分の属性をカスタマイズしたいです.簡単な例を挙げて、プロジェクトの中の複数のインターフェースは自分のカスタマイズしたViewを使います.各インターフェイスはViewの色をカスタマイズするべきです.この時はカスタム属性がないと、違った色のViewを構築する必要がありますか?ですから、私たちは自分の属性をカスタマイズして、私たちの需要に応じてカスタマイズする必要があります.属性はvaluesの下でatrs.xmlファイルを作成します.その中で定義したい属性を定義して、そしてカスタムViewの中で相応の修正をします.私たちはやはり小さい例を使ってカスタムViewとカスタム属性の使用を見てみます.
今日はみんなを連れて、自分で進捗のある円形の進捗状況を定義します.効果を見てみましょう.
上から見ると、私たちは円環の色、円環の進捗度の色をカスタマイズできますが、進捗率、進捗率の色、進捗度が実心か中空かなどを表示しますか?次に私達は皆さんにどう定義するかを教えます.
1.valuesの下にatrs.xmlを新たに作成します.ここでは私たちの属性を定義します.異なる属性は異なるformatに対応しています.属性に対応するformatはhttp://blog.csdn.net/pgalxx/article/details/6766677を参照してもいいです.紹介したのはまだ詳細です.次に私はカスタムのこのプログレスバーで使用した属性を貼ります.
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="RoundProgressBar">
<attr name="roundColor" format="color"/>
<attr name="roundProgressColor" format="color"/>
<attr name="roundWidth" format="dimension"></attr>
<attr name="textColor" format="color" />
<attr name="textSize" format="dimension" />
<attr name="max" format="integer"></attr>
<attr name="textIsDisplayable" format="boolean"></attr>
<attr name="style">
<enum name="STROKE" value="0"></enum>
<enum name="FILL" value="1"></enum>
</attr>
</declare-styleable>
</resources>
2.カスタムViewの属性は定義されています.次に属性とコードをどうやって取得して作成しますか?構造方法で私達が定義した関連属性を取得する必要があります.まずcontext.obtainStylodAttributes(atrs、R.style.RoundPrograte Bar)を呼び出してTypediaryを取得して、Tyrayなどの属性を指定します.
roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);
textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
上のコードは、roundColor=mTypediary.get Color(R.style.RoundPrograss BarColor,Color.RED)のようなものです.get Color方法の最初のパラメータはXMLファイルで定義された色です.もし私達がカスタムしたViewに色を定義していないなら、彼は第二のパラメータのデフォルト値、つまりColor.REDを使います.
3.皆さんに分かりやすくするために、私はViewをカスタマイズしたコードを全部貼り付けます.中のコードは私も詳しい注釈があります.
package com.example.roundprogressbar;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.example.circlepregress.R;
/**
* iphone , View,
* @author xiaanming
*
*/
public class RoundProgressBar extends View {
/**
*
*/
private Paint paint;
/**
*
*/
private int roundColor;
/**
*
*/
private int roundProgressColor;
/**
*
*/
private int textColor;
/**
*
*/
private float textSize;
/**
*
*/
private float roundWidth;
/**
*
*/
private int max;
/**
*
*/
private int progress;
/**
*
*/
private boolean textIsDisplayable;
/**
* ,
*/
private int style;
public static final int STROKE = 0;
public static final int FILL = 1;
public RoundProgressBar(Context context) {
this(context, null);
}
public RoundProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
paint = new Paint();
TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.RoundProgressBar);
//
roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);
textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
mTypedArray.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
*
*/
int centre = getWidth()/2; // x
int radius = (int) (centre - roundWidth/2); //
paint.setColor(roundColor); //
paint.setStyle(Paint.Style.STROKE); //
paint.setStrokeWidth(roundWidth); //
paint.setAntiAlias(true); //
canvas.drawCircle(centre, centre, radius, paint); //
Log.e("log", centre + "");
/**
*
*/
paint.setStrokeWidth(0);
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setTypeface(Typeface.DEFAULT_BOLD); //
int percent = (int)(((float)progress / (float)max) * 100); // , float , 0
float textWidth = paint.measureText(percent + "%"); // ,
if(textIsDisplayable && percent != 0 && style == STROKE){
canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize/2, paint); //
}
/**
* ,
*/
//
paint.setStrokeWidth(roundWidth); //
paint.setColor(roundProgressColor); //
RectF oval = new RectF(centre - radius, centre - radius, centre
+ radius, centre + radius); //
switch (style) {
case STROKE:{
paint.setStyle(Paint.Style.STROKE);
canvas.drawArc(oval, 0, 360 * progress / max, false, paint); //
break;
}
case FILL:{
paint.setStyle(Paint.Style.FILL_AND_STROKE);
if(progress !=0)
canvas.drawArc(oval, 0, 360 * progress / max, true, paint); //
break;
}
}
}
public synchronized int getMax() {
return max;
}
/**
*
* @param max
*/
public synchronized void setMax(int max) {
if(max < 0){
throw new IllegalArgumentException("max not less than 0");
}
this.max = max;
}
/**
* .
* @return
*/
public synchronized int getProgress() {
return progress;
}
/**
* , , ,
* postInvalidate() UI
* @param progress
*/
public synchronized void setProgress(int progress) {
if(progress < 0){
throw new IllegalArgumentException("progress not less than 0");
}
if(progress > max){
progress = max;
}
if(progress <= max){
this.progress = progress;
postInvalidate();
}
}
public int getCricleColor() {
return roundColor;
}
public void setCricleColor(int cricleColor) {
this.roundColor = cricleColor;
}
public int getCricleProgressColor() {
return roundProgressColor;
}
public void setCricleProgressColor(int cricleProgressColor) {
this.roundProgressColor = cricleProgressColor;
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
}
public float getTextSize() {
return textSize;
}
public void setTextSize(float textSize) {
this.textSize = textSize;
}
public float getRoundWidth() {
return roundWidth;
}
public void setRoundWidth(float roundWidth) {
this.roundWidth = roundWidth;
}
}
4.上記のステップを通じて、私達はカスタムViewとカスタムViewの属性を実現しました.もちろん使う過程ではまだ少し変化があります.私達はインターフェースレイアウトの一番上に加えなければなりません.
xmlns:android_custom="http://schemas.android.com/apk/res/com.example.circlepregress"
これは名前空間です.
上の二つのステップで属性を定義できます.カスタムViewを貼ってXMLで使用します.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android_custom="http://schemas.android.com/apk/res/com.example.circlepregress"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.roundprogressbar.RoundProgressBar
android:id="@+id/roundProgressBar2"
android:layout_width="80dip"
android:layout_height="80dip"
android:layout_alignLeft="@+id/roundProgressBar1"
android:layout_alignParentBottom="true"
android:layout_marginBottom="78dp"
android_custom:roundColor="#D1D1D1"
android_custom:roundProgressColor="@android:color/black"
android_custom:textColor="#9A32CD"
android_custom:textIsDisplayable="false"
android_custom:roundWidth="10dip"
android_custom:textSize="18sp"/>
</RelativeLayout>
今日はこれで終わります.もしみなさんに何か質問がありましたら、メッセージをください.
プロジェクトのソースコードをクリックしてダウンロードします。