Android描画简单圆形

最近又变成android开发了。。。上网看了本书,研究了下。。。其实,从qt过渡到android,虽说用的函数不一样。感觉思路都差不多呢。。。。

在activicy中布局:

xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <ImageView
        android:id="@+id/iv"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

androidx.constraintlayout.widget.ConstraintLayout>

在MainActivity.java中添加如下代码:

package leavesc.hello.text_canvas;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView iv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        iv = (ImageView)findViewById(R.id.iv);
        Bitmap bmpBuffer = Bitmap.createBitmap(500,800,Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bmpBuffer);
        Paint paint = new Paint();
        paint.setStyle(Paint.Style.STROKE);
        RectF rect = new RectF(10,10,400,400);
        paint.setColor(Color.GRAY);
        canvas.drawOval(rect,    paint);
        paint.setAntiAlias(true);
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(3);
        canvas.drawArc(rect,    -90,    90,    false,    paint);
        iv.setImageBitmap(bmpBuffer);

    }
}

如果能加上java的timer使其动态描画就能得到圆形进度条的效果了。应该是这个思路吧

Android描画简单圆形_第1张图片

 

转载于:https://www.cnblogs.com/wxmwanggood/p/11282378.html

你可能感兴趣的:(Android描画简单圆形)