畫紅圈
創(chuàng)新互聯(lián)建站專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、永春網(wǎng)絡(luò)推廣、小程序制作、永春網(wǎng)絡(luò)營(yíng)銷、永春企業(yè)策劃、永春品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供永春建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.cdcxhl.com
?xml version="1.0" encoding="utf-8"?
xmlns:android=""
android:shape="oval"
android:useLevel="false"
solid android:color="@color/red" /
? ? android:width="1dp"
? ? android:color="@color/white" /
? ? android:width="20dp"
? ? android:height="20dp" /
/shape
畫黃圈
?xml version="1.0" encoding="utf-8"?
shape xmlns:android=""
android:shape="oval"
android:useLevel="false"
solid android:color="@color/yellow" /
? ? android:width="1dp"
? ? android:color="@color/white" /
? ? android:width="20dp"
? ? android:height="20dp" /
/shape
畫綠圈
?xml version="1.0" encoding="utf-8"?
shape xmlns:android=""
android:shape="oval"
android:useLevel="false"
solid android:color="@color/green" /
? ? android:width="1dp"
? ? android:color="@color/white" /
? ? android:width="20dp"
? ? android:height="20dp" /
/shape
Android里面使用shape設(shè)置控件的外形,例如一些圓角、填充的背景顏色、以及一些漸變的效果等,所以設(shè)置填充顏色,可通過設(shè)置shape.xml文件里的如下屬性:
solid?android:color="@color/common_red"?/
將shape文件放到android的button、textview組件上,就可以有填充背景顏色的效果,完整的代碼如下:
1.shape.xml
?xml?version="1.0"?encoding="UTF-8"?
shape?
xmlns:android=""
android:shape="oval"
android:useLevel="false"?
solid?android:color="@color/common_red"?/
padding?
android:left="2dp"?
android:top="1dp"?
android:right="2dp"?
android:bottom="1dp"?/
solid
android:color="@color/common_red"?/
stroke
android:width="1dp"
android:color="@android:color/white"?/
size?android:width="15dp"
android:height="15dp"?/
/shape
2.把以上代碼添加到drawable里面、通過background引用就可以了
TextView
android:id="@?id/message_category_unread_count"
style="@style/comm_text_style_14_aaaaaa"
android:layout_marginLeft="70dp"
android:layout_marginTop="5dp"
android:background="@drawable/shape"
android:gravity="center"
android:textSize="@dimen/text_size_comment_20"
android:text="7"
android:textColor="@android:color/white"?/
效果如下圖:
public void drawArc(@NonNull RectF oval, float startAngle, float sweepAngle, boolean useCenter,
@NonNull Paint paint) {
drawArc(oval.left, oval.top, oval.right, oval.bottom, startAngle, sweepAngle, useCenter,
paint);
}
1
2
3
4
5
要實(shí)現(xiàn)這個(gè)方法,我們要傳5個(gè)參數(shù)進(jìn)去。
第一個(gè)參數(shù):RectF oval
oval 參數(shù)的作用是:定義的圓弧的形狀和大小的范圍
/**
* 這是一個(gè)居中的圓
*/
float x = (getWidth() - getHeight() / 2) / 2;
float y = getHeight() / 4;
RectF oval = new RectF( x, y,
getWidth() - x, getHeight() - y);
1
2
3
4
5
6
7
8
oval 參數(shù)的作用是:定義的圓弧的形狀和大小的范圍
第二個(gè)參數(shù):float startAngle
這個(gè)參數(shù)的作用是設(shè)置圓弧是從哪個(gè)角度來順時(shí)針繪畫的
canvas.drawArc(oval,-90,120,false,mPaint);
1
-90
canvas.drawArc(oval,90,110,false,mPaint);
1
90
//設(shè)置為-180的時(shí)候也是這樣
canvas.drawArc(oval,180,140,false,mPaint);
1
2
180
//設(shè)置為360的時(shí)候也是這樣
canvas.drawArc(oval,0,140,false,mPaint);
1
2
140
第三個(gè)參數(shù):float sweepAngle
這個(gè)參數(shù)的作用是設(shè)置圓弧掃過的角度
我們從上面的代碼就可以知道其中的作用了
第四個(gè)參數(shù):boolean useCenter
主要運(yùn)用SurfaceView來實(shí)現(xiàn)在屏幕上畫一個(gè)圓,你可以通過按方向鍵和觸摸屏幕來改變圓的位置代碼: Activitypackage com.view;import android.app.Activity;import android.os.Bundle;import android.view.Window;import android.view.WindowManager;public class MainActivity extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// 隱藏狀態(tài)欄this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 把Activity的標(biāo)題去掉requestWindowFeature(Window.FEATURE_NO_TITLE);// 設(shè)置布局this.setContentView(new MySurfaceView(this));}}SurfaceViewpackage com.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.view.KeyEvent;import android.view.MotionEvent;import android.view.SurfaceHolder;import android.view.SurfaceHolder.Callback;import android.view.SurfaceView;public class MySurfaceView extends SurfaceView implements Runnable, Callback {private SurfaceHolder mHolder; // 用于控制SurfaceViewprivate Thread t; // 聲明一條線程private boolean flag; // 線程運(yùn)行的標(biāo)識(shí),用于控制線程private Canvas mCanvas; // 聲明一張畫布private Paint p; // 聲明一支畫筆private int x = 50, y = 50, r = 10; // 圓的坐標(biāo)和半徑public MySurfaceView(Context context) {super(context);mHolder = getHolder(); // 獲得SurfaceHolder對(duì)象mHolder.addCallback(this); // 為SurfaceView添加狀態(tài)監(jiān)聽p = new Paint(); // 創(chuàng)建一個(gè)畫筆對(duì)象p.setColor(Color.WHITE); // 設(shè)置畫筆的顏色為白色setFocusable(true); // 設(shè)置焦點(diǎn)}/*** 自定義一個(gè)方法,在畫布上畫一個(gè)圓*/public void Draw() {mCanvas = mHolder.lockCanvas(); // 獲得畫布對(duì)象,開始對(duì)畫布畫畫mCanvas.drawRGB(0, 0, 0); // 把畫布填充為黑色mCanvas.drawCircle(x, y, r, p); // 畫一個(gè)圓mHolder.unlockCanvasAndPost(mCanvas); // 完成畫畫,把畫布顯示在屏幕上}/*** 當(dāng)SurfaceView創(chuàng)建的時(shí)候,調(diào)用此函數(shù)*/@Overridepublic void surfaceCreated(SurfaceHolder holder) {t = new Thread(this); // 創(chuàng)建一個(gè)線程對(duì)象flag = true; // 把線程運(yùn)行的標(biāo)識(shí)設(shè)置成truet.start(); // 啟動(dòng)線程}/*** 當(dāng)SurfaceView的視圖發(fā)生改變的時(shí)候,調(diào)用此函數(shù)*/@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}/*** 當(dāng)SurfaceView銷毀的時(shí)候,調(diào)用此函數(shù)*/@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {flag = false; // 把線程運(yùn)行的標(biāo)識(shí)設(shè)置成false}/*** 當(dāng)屏幕被觸摸時(shí)調(diào)用*/@Overridepublic boolean onTouchEvent(MotionEvent event) {x = (int) event.getX(); // 獲得屏幕被觸摸時(shí)對(duì)應(yīng)的X軸坐標(biāo)y = (int) event.getY(); // 獲得屏幕被觸摸時(shí)對(duì)應(yīng)的Y軸坐標(biāo)return true;}/*** 當(dāng)用戶按鍵時(shí)調(diào)用*/@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode == KeyEvent.KEYCODE_DPAD_UP){ //當(dāng)用戶點(diǎn)擊↑鍵時(shí)y--; //設(shè)置Y軸坐標(biāo)減1}return super.onKeyDown(keyCode, event);}@Overridepublic void run() {while (flag) {Draw(); // 調(diào)用自定義畫畫方法try {Thread.sleep(50); // 讓線程休息50毫秒} catch (InterruptedException e) {e.printStackTrace();}}}}MySurfaceView首先繼承SurfaceView,然后實(shí)現(xiàn)Runnable和Callback接口重寫了Runnable的run方法和Callback的surfaceCreated(SurfaceHolder holder),surfaceChanged(SurfaceHolder holder, int format, int width,int height),surfaceDestroyed(SurfaceHolder holder)方法, 還實(shí)現(xiàn)了onTouchEvent(MotionEvent event),onKeyDown(int keyCode, KeyEvent event)方法來,詳細(xì)的在代碼里已注釋。
Android繪制兩層圓環(huán),可以使用自定義View,繼承View,重寫里面的Ondraw方法,花兩個(gè)同心圓,示例如下:
package?com.cn.myvdun;??
import?android.content.Context;??
import?android.graphics.Canvas;??
import?android.graphics.Paint;??
import?android.util.AttributeSet;??
import?android.view.View;??
public?class?RingView?extends?View?{??
private?final??Paint?paint;??
private?final?Context?context;??
public?RingView(Context?context)?{??
//?TODO?Auto-generated?constructor?stub??
this(context,?null);??
}??
public?RingView(Context?context,?AttributeSet?attrs)?{??
super(context,?attrs);??
//?TODO?Auto-generated?constructor?stub??
this.context?=?context;??
this.paint?=?new?Paint();??
this.paint.setAntiAlias(true);?//消除鋸齒??
this.paint.setStyle(Paint.Style.STROKE);?//繪制空心圓???
}??
@Override??
protected?void?onDraw(Canvas?canvas)?{??
//?TODO?Auto-generated?method?stub??
int?center?=?getWidth()/2;??
int?innerCircle?=?dip2px(context,?83);?//設(shè)置內(nèi)圓半徑??
int?ringWidth?=?dip2px(context,?5);?//設(shè)置圓環(huán)寬度??
//繪制內(nèi)圓??
this.paint.setARGB(155,?167,?190,?206);??
this.paint.setStrokeWidth(2);??
canvas.drawCircle(center,center,?innerCircle,?this.paint);??
//繪制圓環(huán)??
this.paint.setARGB(255,?212?,225,?233);??
this.paint.setStrokeWidth(ringWidth);??
canvas.drawCircle(center,center,?innerCircle+1+ringWidth/2,?this.paint);??
//繪制外圓??
this.paint.setARGB(155,?167,?190,?206);??
this.paint.setStrokeWidth(2);??
canvas.drawCircle(center,center,?innerCircle+ringWidth,?this.paint);
super.onDraw(canvas);??
}??
}
繪制圓的話,drawcircle需要傳圓心坐標(biāo)和半徑大小/畫筆進(jìn)去,我猜你缺少對(duì)應(yīng)的參數(shù),可以參考如下代碼:
mPaint.setAntiAlias(false); //設(shè)置畫筆為無鋸齒
mPaint.setColor(Color.BLACK); //設(shè)置畫筆顏色
canvas.drawColor(Color.WHITE); //白色背景
mPaint.setStrokeWidth((float) 3.0); //線寬
mPaint.setStyle(Paint.Style.STROKE); //空心效果
canvas.drawCircle(50, 50, 10, mPaint); //繪制圓形
canvas.drawCircle(100, 100, 20, mPaint); //繪制圓形
canvas.drawCircle(150, 150, 30, mPaint); //繪制圓形
canvas.drawCircle(200, 200, 40, mPaint); //繪制圓形
canvas.drawCircle(250, 250, 50, mPaint); //繪制圓形
canvas.drawCircle(300, 300, 60, mPaint); //繪制圓形
canvas.drawCircle(350, 350, 70, mPaint); //繪制圓形