Android 사용자 정의 원형 진행 막대 효과
1 컨트롤 RoundProgress
package listview.tianhetbm.p2p.ui;
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.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import listview.tianhetbm.p2p.R;
/**
* @date:2015/9/14
* @author:dongxiaogang
* @description:
*/
public class RoundProgress extends View {
private Paint paint = new Paint();
private int roundColor;
private int roundProgressColor;
private int textColor;
private float textSize;
private float roundWidth;
private int max = 100;
private int progress = 50;
public RoundProgress(Context context) {
this(context, null);
}
public RoundProgress(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RoundProgress(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);
//
roundColor = ta.getColor(R.styleable.RoundProgress_roundColor, Color.RED);
//
roundProgressColor = ta.getColor(R.styleable.RoundProgress_roundProgressColor, Color.GREEN);
//
textColor = ta.getColor(R.styleable.RoundProgress_textColor, Color.GREEN);
//
textSize = ta.getDimension(R.styleable.RoundProgress_textSize, 15);
//
roundWidth = ta.getDimension(R.styleable.RoundProgress_roundWidth, 5);
ta.recycle();
}
@Override
protected void onDraw(Canvas canvas) {
// :
paint.setColor(roundColor);
paint.setStrokeWidth(roundWidth);
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);
int center = getWidth() / 2;
int radius = (int) (center - roundWidth / 2-45);
//canvas.drawCircle(center, center, radius, paint);
RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);
canvas.drawArc(oval, 135, 270, false, paint);
// :
float textWidth = paint.measureText(progress + "%");
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setStrokeWidth(0);
canvas.drawText(progress + "%", center - textWidth / 2, center + textSize / 2, paint);
// :
/**
* :
* oval:
* 0:
* 360 * progress / max:
* false:
* paint:
*/
//RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);
paint.setColor(roundProgressColor);
paint.setStrokeWidth(roundWidth);
paint.setStyle(Paint.Style.STROKE);
canvas.drawArc(oval, 135, 270 * progress / max, false, paint);
Log.e(" ",(270 * progress / max)+"");
Paint mp=new Paint();
mp.setAntiAlias(true);
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.tiger);
int bitmapHeight = bitmap.getHeight()/2;
int bitmapWidth = bitmap.getWidth()/2;
//canvas.translate(-center, center);
float y=0f,x=0f;
// if(270 * progress / max<=45){
y = (float) (center-bitmapWidth - (radius) * Math.cos((270 * progress / max+225)*Math.PI/180));
x = (float) (center-bitmapWidth + (radius) * Math.sin((270 * progress / max+225)*Math.PI/180));
// }
//canvas.translate(center, center*2);
Log.e(" ", y + "-----" + x);
canvas.drawBitmap(bitmap, x, y, mp);
}
public void setProgress(int progress){
this.progress = progress;
if(progress>100){
this.progress = 100;
}
postInvalidate();
}
}
2 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"
android:gravity="center_horizontal">
<listview.tianhetbm.p2p.ui.RoundProgress
android:layout_marginTop="30dp"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_marginLeft="10dp"
app:roundColor="@color/back_blue"
app:roundProgressColor="@color/back_orange"
android:id="@+id/ce"
app:roundWidth="10dp"
app:textSize="18sp"
app:textColor="@color/record_red"
/>
</RelativeLayout>
3 activity(주요 코드)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_three);
ButterKnife.bind(this);
new Thread(){
@Override
public void run() {
while (progress<80){
progress+=1;
ce.setProgress(progress);
try {
Thread.sleep(50);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}.start();
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.