Android不同形状切换

本文介绍如何在Android中实现不同形状的切换效果,包括详细的java代码实现和控件的使用方法,附带源码下载地址。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android不同形状切换

一、效果图

在这里插入图片描述

二、代码实现

2.1 java代码

package com.example.layoutdemo.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.IntDef;
import androidx.annotation.Nullable;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

public class ShapeView extends View {

    private Paint mPaint;
    private Path mPath;
    private static final int SQUARE = 1;
    private static final int CIRCLE = 2;
    private static final int TRIANGLE = 3;

    private int mType = SQUARE;
    @IntDef({SQUARE,CIRCLE,TRIANGLE})
    @Retention(RetentionPolicy.SOURCE)
    private @interface ShareType{

    }
    public void setType(@ShareType int type) {
        mType = type;
    }
    public ShapeView(Context context) {
        this(context,null);
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs) {
        this(context,attrs,0);
    }

    public ShapeView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeWidth(10);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int size = widthSize > heightSize?heightSize:widthSize;
        setMeasuredDimension(size,size);

    }

    @Override
    protected void onDraw(Canvas canvas) {
        int center = getWidth()/2;
        switch (mType){
            case SQUARE:
                mPaint.setColor(Color.RED);
                canvas.drawRect(0,0,getWidth(),getHeight(),mPaint);
                break;
            case CIRCLE:
                mPaint.setColor(Color.BLUE);
                canvas.drawOval(0,0,getWidth(),getHeight(),mPaint);
                break;
            case TRIANGLE:
                mPaint.setColor(Color.BLACK);
                if (mPath == null) {
                    mPath = new Path();
                    mPath.moveTo(center,0);
                    mPath.lineTo(0, (float) (center * Math.sqrt(3)));
                    mPath.lineTo(getWidth(),(float) (center * Math.sqrt(3)));
                    mPath.close();
                }
                canvas.drawPath(mPath,mPaint);
                break;
        }

    }

    public synchronized void start() {
        if (mType == SQUARE) {
            mType = CIRCLE;
        } else if (mType == CIRCLE) {
            mType = TRIANGLE;
        } else {
            mType = SQUARE;
        }
        postInvalidate();
    }

}

2.2 控件使用

 <com.example.layoutdemo.widget.ShapeView
        android:id="@+id/shape_view"
        android:layout_margin="20dp"
        android:layout_width="200dp"
        android:layout_height="200dp"/>
  final ShapeView shape_view= findViewById(R.id.shape_view);
        new Thread(new Runnable() {
            @Override
            public void run() {
                while(true) {
                    shape_view.start();
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

三、源码下载地址

Android自定义View

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值