温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android怎么自定义View实现风车效果

发布时间:2022-08-17 16:25:45 来源:亿速云 阅读:141 作者:iii 栏目:开发技术

本篇内容介绍了“Android怎么自定义View实现风车效果”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

效果图:

Android怎么自定义View实现风车效果

画杆

public class WindmillRodView extends View {

    private int mWidth;
    private int mHeight;
    private Paint mPaint;

    public WindmillRodView(Context context) {
        this(context, null);
    }

    public WindmillRodView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WindmillRodView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    private int _rod_width = dp2px(2);

    @Override
    protected void onDraw(Canvas canvas) {
        int xCenter = mWidth / 2;
        int yCenter = mHeight / 3;
        int radius = mHeight / 3 * 2;
        drawRod(canvas, xCenter, yCenter, radius);
    }

    private void drawRod(Canvas canvas, int xCenter, int yCenter, int radius) {
        Path path = new Path();
        path.moveTo(xCenter - _rod_width, yCenter);
        path.lineTo(xCenter - 2 * _rod_width, radius - dp2px(5));
        path.lineTo((xCenter + 2 * _rod_width), radius - dp2px(5));
        path.lineTo(xCenter + _rod_width, yCenter);
        path.close();
        canvas.drawPath(path, mPaint);
        RectF rectF = new RectF(xCenter - 2 * _rod_width,
                radius - dp2px(8),
                xCenter + 2 * _rod_width,
                radius - dp2px(3));
        canvas.drawOval(rectF, mPaint);
    }

    private int dp2px(int dp) {
        return (int) (Resources.getSystem().getDisplayMetrics().density * dp + 0.5);
    }
}

先画风车的杆,再在底部画一个椭圆

画风车

public class WindmillView extends View {

    private int mWidth;
    private int mHeight;
    private Paint mPaint;

    private ObjectAnimator mRotationAnim;

    public WindmillView(Context context) {
        this(context, null);
    }

    public WindmillView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WindmillView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint();
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = getMeasuredWidth();
        mHeight = getMeasuredHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int xCenter = mWidth / 2;
        int yCenter = mHeight / 3;
        int radius = mHeight / 3 * 2;
        canvas.drawCircle(xCenter, yCenter - dp2px(7), dp2px(4), mPaint);
        setPivotX(xCenter);
        setPivotY(yCenter - dp2px(7));
        canvas.save();
        for (int i = 0; i < 3; i++) {
            Path path = new Path();
            path.moveTo(xCenter, 0);
            path.lineTo(xCenter, yCenter - dp2px(11));
            path.lineTo(xCenter + dp2px(8), yCenter - dp2px(26));
            path.close();
//        mPaint.setStrokeJoin(Paint.Join.ROUND);
            CornerPathEffect cornerPathEffect = new CornerPathEffect(30);
            mPaint.setPathEffect(cornerPathEffect);
            canvas.drawPath(path, mPaint);
            canvas.rotate(360 / 3, xCenter, yCenter - dp2px(7));
        }
        canvas.restore();
        startAnim();
    }

    private int dp2px(int dp) {
        return (int) (Resources.getSystem().getDisplayMetrics().density * dp + 0.5);
    }

    public void startAnim() {
        if (mRotationAnim != null && mRotationAnim.isRunning()) return;
        mRotationAnim = ObjectAnimator.ofFloat(this, "Rotation", 360f)
                .setDuration(3000);
        mRotationAnim.setRepeatCount(-1);
        mRotationAnim.setInterpolator(new LinearInterpolator());
        mRotationAnim.start();
    }

    public void stopAnim() {
        if (mRotationAnim != null && mRotationAnim.isRunning()) {
            mRotationAnim.cancel();
            mRotationAnim = null;
        }
    }
}

这里使用画面的旋转方法,绘制扇页

旋转

使用属性动画来旋转

....
 public void startAnim() {
        if (mRotationAnim != null && mRotationAnim.isRunning()) return;
        mRotationAnim = ObjectAnimator.ofFloat(this, "Rotation", 360f)
                .setDuration(3000);
        mRotationAnim.setRepeatCount(-1);
        mRotationAnim.setInterpolator(new LinearInterpolator());
        mRotationAnim.start();
    }

    public void stopAnim() {
        if (mRotationAnim != null && mRotationAnim.isRunning()) {
            mRotationAnim.cancel();
            mRotationAnim = null;
        }
    }
 ....

在布局文件中使用

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <com.example.windmill.WindmillRodView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true" />

    <com.example.windmill.WindmillView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerInParent="true" />

</RelativeLayout>

这里只是介绍了如何绘制类似的效果,很多计算都是写死的,如果要实际使用的话,最好写成自定义属性通过xml属性声明传进去。

“Android怎么自定义View实现风车效果”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI