Android 自定义View的简单应用(1) 雷达波效果

效果如下:

布局文件:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="20dp"
    android:background="#000000"
    tools:context=".MainActivity">

    <myapp.customview.CustomView.RadarView
    android:id="@+id/radar"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</RelativeLayout>

RadarView.java

public class RadarView extends View {

    private int CenterX = 0;
    private int CenterY = 0;

    private int radius1 = 0;
    private int radius2 = 0;

    private int space = 200;
    private int grid_size_width = 5;
    private int grid_size_height = 9;
    private float CenterImageAngle = 0f;
    private float LineAngle = 0f;
    private boolean run = true;
    private boolean start = false;
    private int[] colors = new int[]{Color.parseColor("#00FF00"),Color.parseColor("#00FF33"),Color.parseColor("#00CC00")
    ,Color.parseColor("#00CC33"),Color.parseColor("#009900"),Color.parseColor("#009933")};
    private int Color_Index = 0;

    public RadarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        mRunnable runnable = new mRunnable();
        Thread thread = new Thread(runnable);
        thread.start();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        int width = this.getMeasuredWidth();
        int height = this.getMeasuredHeight();
        CenterX = width / 2;
        CenterY = height / 2;

        /*
        背景色 黑色
         */
        canvas.drawColor(Color.BLACK);

        /*
        绘制网格 grid
         */
        Paint gridPaint = new Paint();
        gridPaint.setStrokeWidth(3);
        gridPaint.setColor(Color.DKGRAY);

        int space = height / grid_size_height;
        ArrayList<Float> linesList = new ArrayList<>();
        for(int i = 1;i < grid_size_height;i++)
        {
            linesList.add(0f);
            linesList.add((float) (space * i));
            linesList.add((float) width);
            linesList.add((float) (space * i));
        }

        int space2 = width / grid_size_width;
        for(int i = 1;i < grid_size_width;i++)
        {
            linesList.add((float) (space2 * i));
            linesList.add(0f);
            linesList.add((float) (space2 * i));
            linesList.add((float)height);
        }

        int len = linesList.size();
        float[] lines = new float[len];
        for(int i = 0;i < len;i++)
        {
            lines[i] = linesList.get(i);
        }

        canvas.drawLines(lines,gridPaint);


        /*
        绘制雷达波
         */
        Paint paint = new Paint();
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        canvas.drawCircle(CenterX,CenterY,radius1,paint);
        canvas.drawCircle(CenterX,CenterY,radius2,paint);

        radius1 = radius1 + 5;
        if(radius1 > width / 2)
        {
            start = true;
        }
        if(start)
        {
            radius2 = radius2 + 5;
        }


        if(radius1 > width && radius1 > height)
        {
            radius1 = 0;
        }
        if(radius2 > width && radius2 > height)
        {
            radius2 = 0;
        }

        /*
        绘制中心人物
         */
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.center);
        Matrix matrix = new Matrix();
        matrix.postRotate(CenterImageAngle,bitmap.getWidth() / 2,bitmap.getWidth() / 2);
        matrix.postTranslate(CenterX - bitmap.getWidth() / 2,CenterY - bitmap.getHeight() / 2);

        canvas.drawBitmap(bitmap,matrix,new Paint());

        /*
        绘制目标点
         */
        Paint mpaint = new Paint();
        mpaint.setColor(colors[Color_Index]);
        mpaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(100,100,20,mpaint);

        invalidate();
    }

    private class mRunnable implements Runnable
    {
        @Override
        public void run() {
            while(run)
            {
                CenterImageAngle = CenterImageAngle + 5;
                if(CenterImageAngle > 360)
                    CenterImageAngle = 0;

                Color_Index++;
                if(Color_Index >= colors.length)
                    Color_Index = 0;

                try {
                    Thread.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值