3つのカスタムviewレイアウト:長方形TextView、円形プログレスバー、リングview
44390 ワード
注意:この3つのカスタムviewレイアウトはxmlファイルを共有しています.
1.カスタム長方形TextViewのview
*/
1.カスタム長方形TextViewのview
package com.bwie.CustomView.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.widget.TextView;
import com.bwie.CustomView.R;
/**
TextView view
*/
public class CustomTextView extends TextView{
private String text;
private int color;
private Paint paint;
private int size;
private Rect rect;
public CustomTextView(Context context) {
super(context);
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// context.obtainStyledAttributes() view
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomTextView);
// view
int count = typedArray.getIndexCount();
// view
for (int i=0;iint indexAttr = typedArray.getIndex(i);
switch (indexAttr){
// view text
case R.styleable.CustomTextView_text:
text = typedArray.getString(indexAttr);
break;
// view color ,
case R.styleable.CustomTextView_textColor:
color = typedArray.getInt(indexAttr, Color.YELLOW);
break;
// view size
case R.styleable.CustomTextView_textSize:
// 2 size
DisplayMetrics metrics = getResources().getDisplayMetrics();
float dimension = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 18, metrics);
size = typedArray.getDimensionPixelSize(indexAttr, (int) dimension);
break;
}
}
// ,
typedArray.recycle();
// size
paint = new Paint();
paint.setTextSize(size);
// 。 text
rect = new Rect();
paint.getTextBounds(text,0,text.length(),rect);
}
/**
* onMeasure : , , View
widthMeasureSpec: widthMeasureSpec
heightMeasureSpec: heightMeasureSpec
onMeasure , mode :
(1)EXACTLY: (100dp) MATCH_PARENT;
(2)AT_MOST: , WARP_CONTENT;
(3)UNSPECIFIED: , ;
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// view
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
// view size
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
System.out.println("width = " + width);
System.out.println("height = " + height);
}
//onDraw : (canvas )
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//getWidth(): View , View getHeight(): View , View
int widht = getWidth();
int height = getHeight() ;
//
int rectWidth = rect.width();
int rectHeight = rect.height();
//
paint.setColor(Color.YELLOW);
//drawRect , 。
canvas.drawRect(0,0,getMeasuredWidth(),getMeasuredHeight(),paint);
// ,
paint.setColor(color);
/**
* drawText : 。 :
* text:
* x: x
* y: y
* paint:
*/
canvas.drawText(text,(getWidth()-rect.width())/2,(getHeight()+rect.height())/2,paint);
System.out.println("getWidth() = " + getWidth());
System.out.println("getMeasuredWidth() = " + getMeasuredWidth());
}
/**
*
* @param w view
* @param h view
* @param oldw view
* @param oldh view
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
System.out.println("w = " + w + " " + h + " " + oldw + " " + oldh);
}
public CustomTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
※ values TextView xml
xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="CustomTextView"> <attr name="text" format="string"/> <attr name="textSize" format="dimension"/> <attr name="textColor" format="color"/> declare-styleable> resources>
2. カスタム円形進捗バーviewpackage com.bwie.CustomView.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.view.View; /** * view */ public class CustomProgressView extends View{ private boolean runing = true; private int progress = 0; private Paint paint; public CustomProgressView(Context context) { super(context); } public CustomProgressView(Context context, AttributeSet attrs) { super(context, attrs); // paint = new Paint(); // paint.setColor(Color.BLUE); // style: paint.setStyle(Paint.Style.STROKE); // , new Thread(new Runnable() { @Override public void run() { while (runing){ if (progress >= 360){ runing = false; return; } progress += 10; // :postInvalidate(), onDraw() postInvalidate(); try { Thread.sleep(188); }catch (Exception e){ e.printStackTrace(); } } } }).start(); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //(1) view int x = getWidth() / 2; int y = getHeight() / 2; //(2) int radius = 300; //(3) paint.setStrokeWidth(18); //(4) :RectF float RectF rectF = new RectF(x - radius, y - radius, x + radius, y + radius); /** * :drawArc : , , 、 、 。 * oval: 。 startAngle: 。 sweepAngle: 。 useCenter: ,true ,false 。 paint: 。 */ canvas.drawArc(rectF,-90,progress,false,paint); //(5) progress int int intProgress = (int) ((float)progress/360 * 100); //(6) measureText float textWidth = paint.measureText(intProgress + "%"); // :Rect integer Rect rect = new Rect(); // paint.getTextBounds(intProgress+"%",0,(intProgress+"%").length(),rect); // size paint.setTextSize(90); paint.setStrokeWidth(1); // rect.height() /** * drawText : * text: * x: x * y: y * paint: */ canvas.drawText(intProgress+"%",x-textWidth/2,y+rect.height()/2,paint); } public CustomProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } }
3. カスタムリングviewpackage com.bwie.CustomView.view; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; /** * view */ public class CustomCircleView extends View{ private Paint paint; private int xCircle = 200; // x private int yCircle = 200; // y public CustomCircleView(Context context) { super(context); } //CustomCircleView : view public CustomCircleView(Context context, AttributeSet attrs) { super(context, attrs); // ,style,setAntiAlias (true ,false ),setStrokeWidth : paint = new Paint(); paint.setColor(Color.BLUE); paint.setStyle(Paint.Style.STROKE); paint.setAntiAlias(true); paint.setStrokeWidth(80); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()){ case MotionEvent.ACTION_DOWN: //getRawX() getRawY() System.out.println(" : x "+event.getRawX()+" y "+event.getRawY()); break; case MotionEvent.ACTION_MOVE: //getRawX() getRawY() System.out.println(" : x "+event.getRawX()+" y "+event.getRawY()); // , X Y :getX() getY() view xCircle = (int) event.getX(); yCircle = (int)event.getY(); /** * Android Invalidate , Invalidate , : 1. Android UI , UI 。 invalidate() View , UI 。 view , invalidate() 。invalidate() view UI pop 。 2.Android , 。 , UI 、 UI , onCreate() UI , UI 。 。invalidate() UI , Handler UI 。 postInvalidate() 。 */ invalidate(); break; case MotionEvent.ACTION_UP: //getRawX() getRawY() System.out.println(" : x "+event.getRawX()+" y "+event.getRawY()); break; } return true; } /** * onMeasure : , widthMeasureSpec: widthMeasureSpec heightMeasureSpec: heightMeasureSpec */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } //onDraw : (canvas ) @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); /** * canvas.drawCircle() : , 。 , 。 * cx: x 。 cy: y 。 radius: 。 paint: 。 */ canvas.drawCircle(xCircle,yCircle,200,paint); } public CustomCircleView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } }
4. 3つのカスタムview共通xmlファイルxml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" > <com.bwie.CustomView.view.CustomTextView android:layout_centerInParent="true" android:layout_marginTop="28dp" android:layout_width="188dp" android:layout_height="188dp" app:textSize="18sp" app:text=" TextView" app:textColor="@color/colorAccent" /> RelativeLayout>