电商_自定义刮刮乐

博客围绕布局、Activity和自定义View展开,可能介绍了Activity中的布局方式以及如何自定义View,还可能展示了相关效果图,属于移动开发领域的内容。

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

布局

<?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=".activity.GuaActivity">

    <com.example.myapplication.view.ActionBarView
        android:layout_width="match_parent"
        android:layout_height="50dp"/>

    <com.example.myapplication.view.GuaGuaView
        android:layout_marginTop="50dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</RelativeLayout>

Activity

package com.example.myapplication.activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.example.myapplication.R;

//刮刮乐
public class GuaActivity extends AppCompatActivity {

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

自定义View

package com.example.myapplication.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


public class GuaGuaView extends View {
    private Paint mPaint,mPaint2;
    private Bitmap bitMap;
    private Canvas canvas;
    private Path mPath;

    public GuaGuaView(Context context) {
        super(context);
        init(context);
    }

    public GuaGuaView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }


    //初始化 画笔  路径等
    private void init(Context context) {

        mPath = new Path();

        mPaint = new Paint();
        mPaint.setTextSize(36);
        mPaint.setColor(Color.RED);
        mPaint.setTextAlign(Paint.Align.CENTER);

        mPaint2=new Paint();
        mPaint2.setStrokeWidth(30);
        mPaint2.setAntiAlias(true);
        mPaint2.setAlpha(0);//设置透明度
        mPaint2.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        mPaint2.setStyle(Paint.Style.STROKE);//设置为空心
    }

    //绘制
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvasText(canvas);

        canvas.drawBitmap(bitMap, 0, 0, null);
    }

    //绘制中奖信息
    private void canvasText(Canvas canvas) {
        canvas.drawText("恭喜您,中了特等奖", getRight() / 2, getBottom() / 2, mPaint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        bitMap = Bitmap.createBitmap(MeasureSpec.getSize(widthMeasureSpec), MeasureSpec.getSize(heightMeasureSpec), Bitmap.Config.ARGB_8888);
        canvas = new Canvas(bitMap);
        canvas.drawColor(Color.parseColor("#999999"));
    }

    //监听手势
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath.moveTo(event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.lineTo(event.getX(), event.getY());
                break;
        }

        canvas.drawPath(mPath,mPaint2);
        invalidate();//更新
        return true;
    }
}

效果图
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值