android自定义控件的学习

这阶段忙着做实验,更新就搁置下来了。。。

同时学习了android自定义控件,下面介绍水波纹的具体写法:

package com.example111.sxy.a09;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by sxy on 2017/10/26.
 */

public class WaveView extends View {
    private Paint paint;
    private int radio = 5;

    public WaveView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
//画笔
        radio=5;
        paint = new Paint();
        paint.setColor(Color.RED);
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(radio/3);
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            radio +=5;
            int alpha = paint.getAlpha();
            alpha -=5;
            if(alpha <0) {
                alpha = 0;
            }
            paint.setAlpha(alpha);
            paint.setStrokeWidth(radio/3);
            invalidate();
        }
    };
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(paint.getAlpha()>0 && downX>0 &&downY>0) {
            canvas.drawCircle(downX,downY,radio,paint);
            handler.sendEmptyMessageDelayed(0,50);
        }
    }
    private float downX;
    private float downY;
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case  MotionEvent.ACTION_DOWN:
                downX = event.getX();
                downY = event.getY();
                radio=5;
                paint = new Paint();
                paint.setColor(Color.RED);
                paint.setAntiAlias(true);
                paint.setStyle(Paint.Style.STROKE);
                paint.setStrokeWidth(radio/3);
                invalidate();


                break;
        }

        return super.onTouchEvent(event);
    }
}
<?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" tools:context="com.example111.sxy.a09.MainActivity">

<com.example111.sxy.a09.WaveView android:layout_height="match_parent"
    android:layout_width="match_parent" />

</RelativeLayout>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值