扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
画红圈
创新互联建站专注为客户提供全方位的互联网综合服务,包含不限于成都网站设计、网站制作、永春网络推广、小程序制作、永春网络营销、永春企业策划、永春品牌公关、搜索引擎seo、人物专访、企业宣传片、企业代运营等,从售前售中售后,我们都将竭诚为您服务,您的肯定,是我们最大的嘉奖;创新互联建站为所有大学生创业者提供永春建站搭建服务,24小时服务热线:18980820575,官方网址: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设置控件的外形,例如一些圆角、填充的背景颜色、以及一些渐变的效果等,所以设置填充颜色,可通过设置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
要实现这个方法,我们要传5个参数进去。
第一个参数:RectF oval
oval 参数的作用是:定义的圆弧的形状和大小的范围
/**
* 这是一个居中的圆
*/
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 参数的作用是:定义的圆弧的形状和大小的范围
第二个参数:float startAngle
这个参数的作用是设置圆弧是从哪个角度来顺时针绘画的
canvas.drawArc(oval,-90,120,false,mPaint);
1
-90
canvas.drawArc(oval,90,110,false,mPaint);
1
90
//设置为-180的时候也是这样
canvas.drawArc(oval,180,140,false,mPaint);
1
2
180
//设置为360的时候也是这样
canvas.drawArc(oval,0,140,false,mPaint);
1
2
140
第三个参数:float sweepAngle
这个参数的作用是设置圆弧扫过的角度
我们从上面的代码就可以知道其中的作用了
第四个参数:boolean useCenter
主要运用SurfaceView来实现在屏幕上画一个圆,你可以通过按方向键和触摸屏幕来改变圆的位置代码: 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);// 隐藏状态栏this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);// 把Activity的标题去掉requestWindowFeature(Window.FEATURE_NO_TITLE);// 设置布局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; // 线程运行的标识,用于控制线程private Canvas mCanvas; // 声明一张画布private Paint p; // 声明一支画笔private int x = 50, y = 50, r = 10; // 圆的坐标和半径public MySurfaceView(Context context) {super(context);mHolder = getHolder(); // 获得SurfaceHolder对象mHolder.addCallback(this); // 为SurfaceView添加状态监听p = new Paint(); // 创建一个画笔对象p.setColor(Color.WHITE); // 设置画笔的颜色为白色setFocusable(true); // 设置焦点}/*** 自定义一个方法,在画布上画一个圆*/public void Draw() {mCanvas = mHolder.lockCanvas(); // 获得画布对象,开始对画布画画mCanvas.drawRGB(0, 0, 0); // 把画布填充为黑色mCanvas.drawCircle(x, y, r, p); // 画一个圆mHolder.unlockCanvasAndPost(mCanvas); // 完成画画,把画布显示在屏幕上}/*** 当SurfaceView创建的时候,调用此函数*/@Overridepublic void surfaceCreated(SurfaceHolder holder) {t = new Thread(this); // 创建一个线程对象flag = true; // 把线程运行的标识设置成truet.start(); // 启动线程}/*** 当SurfaceView的视图发生改变的时候,调用此函数*/@Overridepublic void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {}/*** 当SurfaceView销毁的时候,调用此函数*/@Overridepublic void surfaceDestroyed(SurfaceHolder holder) {flag = false; // 把线程运行的标识设置成false}/*** 当屏幕被触摸时调用*/@Overridepublic boolean onTouchEvent(MotionEvent event) {x = (int) event.getX(); // 获得屏幕被触摸时对应的X轴坐标y = (int) event.getY(); // 获得屏幕被触摸时对应的Y轴坐标return true;}/*** 当用户按键时调用*/@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if(keyCode == KeyEvent.KEYCODE_DPAD_UP){ //当用户点击↑键时y--; //设置Y轴坐标减1}return super.onKeyDown(keyCode, event);}@Overridepublic void run() {while (flag) {Draw(); // 调用自定义画画方法try {Thread.sleep(50); // 让线程休息50毫秒} catch (InterruptedException e) {e.printStackTrace();}}}}MySurfaceView首先继承SurfaceView,然后实现Runnable和Callback接口重写了Runnable的run方法和Callback的surfaceCreated(SurfaceHolder holder),surfaceChanged(SurfaceHolder holder, int format, int width,int height),surfaceDestroyed(SurfaceHolder holder)方法, 还实现了onTouchEvent(MotionEvent event),onKeyDown(int keyCode, KeyEvent event)方法来,详细的在代码里已注释。
Android绘制两层圆环,可以使用自定义View,继承View,重写里面的Ondraw方法,花两个同心圆,示例如下:
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); //设置内圆半径
int ringWidth = dip2px(context, 5); //设置圆环宽度
//绘制内圆
this.paint.setARGB(155, 167, 190, 206);
this.paint.setStrokeWidth(2);
canvas.drawCircle(center,center, innerCircle, this.paint);
//绘制圆环
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需要传圆心坐标和半径大小/画笔进去,我猜你缺少对应的参数,可以参考如下代码:
mPaint.setAntiAlias(false); //设置画笔为无锯齿
mPaint.setColor(Color.BLACK); //设置画笔颜色
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); //绘制圆形
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流