사용자 정의 컨트롤 5: 모조 안지 시장 핸드폰 정리 기능
다음은 원본 그림입니다.
자, 바로 코드에 들어가세요.
attr.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="firstColor" format="color"></attr>
<attr name="sencondColor" format="color"></attr>
<attr name="cicleWidth" fotmat="dimension"></attr>
<attr name="speed" format="integer"></attr>
<attr name="textSize" format="dimension"></attr>
<!-- -->
<declare-styleable name="CustomProgressBar">
<attr name="firstColor"></attr>
<attr name="sencondColor"></attr>
<attr name="cicleWidth"></attr>
<attr name="speed"></attr>
<attr name="textSize"></attr>
</declare-styleable>
</resources>
CusomProgressBar.java:
package com.example.mycustomwidget_03_4.view;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import com.example.mycustomwidget_03_4.R;
public class CusomProgressBar extends View {
private int firstColor, scondColor;
private int cicleWidth, speed,textSize;
private Paint mPaint;
private int mProgress;
public static boolean isOK;
public CusomProgressBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
// TODO Auto-generated constructor stub
}
public CusomProgressBar(Context context) {
this(context, null);
// TODO Auto-generated constructor stub
}
public CusomProgressBar(Context context, AttributeSet attrs,
int defStyleAttr) {
super(context, attrs, defStyleAttr);
//
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.CustomProgressBar, defStyleAttr, 0);
int n = a.getIndexCount();
for (int i = 0; i < n; i++) {
int attr = a.getIndex(i);
switch (attr) {
case R.styleable.CustomProgressBar_firstColor:
firstColor = a.getColor(attr, Color.BLUE);
break;
case R.styleable.CustomProgressBar_sencondColor:
scondColor = a.getColor(attr, Color.BLUE);
break;
case R.styleable.CustomProgressBar_speed:
speed = a.getInt(attr, 5);
break;
case R.styleable.CustomProgressBar_cicleWidth:
cicleWidth = a.getDimensionPixelSize(attr, 20);
break;
case R.styleable.CustomProgressBar_textSize:
textSize = a.getDimensionPixelSize(attr, 65);
break;
}
}
//
a.recycle();
}
public void setProgress(int progress) {
this.mProgress = progress;
try {
Thread.sleep(speed);
} catch (InterruptedException e) {
e.printStackTrace();
}
postInvalidate();
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
init();
int centre = drawArc(canvas);
drawText(canvas, centre);
drawImg(canvas, centre);
}
/**
*
* @param canvas
* @param centre
*/
private void drawImg(Canvas canvas, int centre) {
//
if(isOK){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_clear_broom);
int bWidth = bitmap.getWidth();
int bHeight = bitmap.getHeight();
canvas.drawBitmap(bitmap, centre-bWidth/2, centre+bHeight, mPaint);
}else{
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_clear_ok);
int bWidth = bitmap.getWidth();
int bHeight = bitmap.getHeight();
canvas.drawBitmap(bitmap, centre-bWidth/2, centre+bHeight, mPaint);
}
}
/**
*
* @param canvas
* @param centre
*/
private void drawText(Canvas canvas, int centre) {
//
mPaint.setColor(Color.WHITE);
mPaint.setTextSize(textSize);
//
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
mPaint.setStrokeWidth(0);
String text = mProgress+"%";
/**
*
*/
float width = mPaint.measureText(text);
/**
*
*/
Rect bounds = new Rect();
mPaint.getTextBounds(text, 0, 1, bounds);
//
int height = bounds.height();
canvas.drawText(text, centre - width / 2, centre+height/2, mPaint);
}
/**
*
* @param canvas
* @return
*/
private int drawArc(Canvas canvas) {
// x
int centre = getWidth() / 2;
//
int radius = centre - cicleWidth / 2;
//
RectF oval = new RectF(cicleWidth / 2, cicleWidth / 2, getWidth()
- cicleWidth / 2, getWidth() - cicleWidth / 2);
//
mPaint.setColor(firstColor);
canvas.drawArc(oval, 135, 270, false, mPaint);
//
mPaint.setColor(scondColor);
canvas.drawArc(oval, 135, (float) (mProgress*2.7), false, mPaint);
return centre;
}
/**
* Paint
*/
private void init() {
mPaint = new Paint();
mPaint.setStrokeWidth(cicleWidth);
mPaint.setAntiAlias(true);
mPaint.setStyle(Style.STROKE);
//
mPaint.setStrokeCap(Paint.Cap.ROUND);
}
}
다음은 MainActivity입니다.java:
package com.example.mycustomwidget_03_4;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import com.example.mycustomwidget_03_4.view.CusomProgressBar;
public class MainActivity extends Activity {
private int mProgress;
private CusomProgressBar pb;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pb = (CusomProgressBar) findViewById(R.id.pb);
}
public void start(View view){
new Thread() {
public void run() {
mProgress = 0;
pb.isOK = true;
while (pb.isOK) {
mProgress++;
if (mProgress == 100) {
pb.isOK = false;
}
pb.setProgress(mProgress);
}
};
}.start();
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fhl="http://schemas.android.com/apk/res/com.example.mycustomwidget_03_4"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#32c264"
android:orientation="vertical"
>
<com.example.mycustomwidget_03_4.view.CusomProgressBar
android:id="@+id/pb"
fhl:textSize="50sp"
fhl:firstColor="#33ff00"
fhl:sencondColor="#ffffff"
fhl:cicleWidth="20dip"
fhl:speed="50"
android:layout_width="200dip"
android:layout_height="200dip"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="69dp" >
<Button
android:onClick="start"
android:text=" "
android:layout_width="wrap_content"
android:layout_height="50dip"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="center" />
</RelativeLayout>
</LinearLayout>
여기서 끝이야.
컴파일 실행 결과는 다음과 같습니다.
원본 다운로드:http://download.csdn.net/detail/yushanfenghailin/8696509
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.