自定义画板 礼物 android,android 自定义 画板

View

package com.restore;

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.util.AttributeSet;

import android.util.DisplayMetrics;

import android.view.MotionEvent;

import android.view.View;

import android.view.WindowManager;

public class myview extends View {

Path mPath;

Bitmap mBitmap;

Canvas mCanvas;

Paint mPaint;

int width;

int height;

int color;

float mX = 0.0f;

float mY = 0.0f;

private Bitmap cachebBitmap;

@Override

public boolean onTouchEvent(MotionEvent event) {

int act = event.getAction();

float x = event.getX();

float y = event.getY();

switch (act) {

case MotionEvent.ACTION_DOWN:

mPath.moveTo(x, y);

break;

case MotionEvent.ACTION_MOVE:

mPath.quadTo(mX, mY, x, y);

break;

case MotionEvent.ACTION_UP:

mCanvas.drawPath(mPath, mPaint);

mPath.reset();

break;

}

invalidate();

mX = x;

mY = y;

return true;

}

@Override

protected void onDraw(Canvas canvas) {

// TODO Auto-generated method stub

super.onDraw(canvas);

// mcanvas = new Canvas(mBitmap);

canvas.drawColor(Color.WHITE);

canvas.drawBitmap(cachebBitmap, 0, 0, null);

canvas.drawPath(mPath, mPaint);

}

public void clear()

{

mCanvas.drawColor(Color.WHITE);

mCanvas.drawBitmap(cachebBitmap, 0, 0, null);

invalidate();

}

public void setColor(int c){

mPaint.setColor(c);

}

public void setBlock(float size)

{

mPaint.setStrokeWidth(size);

}

public myview(Context context) {

super(context);

init();

}

public myview(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

init();

}

public myview(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

public void init(){

DisplayMetrics metrics = new DisplayMetrics();

WindowManager wm = (WindowManager) this.getContext().getSystemService("window");

wm.getDefaultDisplay().getMetrics(metrics);

width = metrics.widthPixels;

height = metrics.heightPixels;

mPaint = new Paint();

mPaint.setAntiAlias(true);

mPaint.setColor(Color.BLUE);

mPaint.setStyle(Paint.Style.STROKE);

mPaint.setStrokeWidth(12);

cachebBitmap = Bitmap.createBitmap(width, height,

Bitmap.Config.ARGB_8888);

mCanvas = new Canvas(cachebBitmap);

mPath = new Path();

}

}

ACTIVITY

package com.restore;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.Path;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.LinearLayout;

public class TestRestoreActivity extends Activity implements OnClickListener {

Path mPath;

Bitmap mBitmap;

Canvas mCanvas;

Paint mPaint;

int width;

int height;

myview m;

Button l1, l2, l3,cred,cbule,cblack;

Button colorBtn = null;

Button clearBtn = null;

Button lineBtn = null;

LinearLayout linelayout, colorlayout = null;

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

height = getWindowManager().getDefaultDisplay().getHeight();

width = getWindowManager().getDefaultDisplay().getWidth();

m = (myview) findViewById(R.id.myview);

linelayout = (LinearLayout) findViewById(R.id.lineBar);

colorlayout = (LinearLayout) findViewById(R.id.colorBar);

colorBtn = (Button) findViewById(R.id.color);

lineBtn = (Button) findViewById(R.id.line);

clearBtn = (Button) findViewById(R.id.clear);

cred = (Button) findViewById(R.id.colorred);

cbule = (Button) findViewById(R.id.colorblue);

cblack = (Button) findViewById(R.id.colorblack);

l1 = (Button) findViewById(R.id.l1);

l3 = (Button) findViewById(R.id.l3);

l2 = (Button) findViewById(R.id.l2);

colorBtn.setOnClickListener(this);

lineBtn.setOnClickListener(this);

clearBtn.setOnClickListener(this);

l1.setOnClickListener(this);

l2.setOnClickListener(this);

l3.setOnClickListener(this);

cred.setOnClickListener(this);

cbule.setOnClickListener(this);

cblack.setOnClickListener(this);

}

@Override

public void onClick(View v) {

if (v == colorBtn) {

if (!colorlayout.isShown()) {

if (linelayout.isShown()) {

linelayout.setVisibility(View.INVISIBLE);

}

colorlayout.setVisibility(View.VISIBLE);

} else {

colorlayout.setVisibility(View.INVISIBLE);

}

} else if (v == lineBtn) {

if (!linelayout.isShown()) {

if (colorlayout.isShown()) {

colorlayout.setVisibility(View.INVISIBLE);

}

linelayout.setVisibility(View.VISIBLE);

} else {

linelayout.setVisibility(View.INVISIBLE);

}

} else if (v == clearBtn) {

m.clear();

} else if (v == cbule) {

m.setColor(Color.BLUE);

colorlayout.setVisibility(View.INVISIBLE);

} else if (v == cred) {

m.setColor(Color.RED);

colorlayout.setVisibility(View.INVISIBLE);

} else if (v == cblack) {

m.setColor(Color.BLACK);

colorlayout.setVisibility(View.INVISIBLE);

}

else if(v == l1)

{

m.setBlock(12.0f);

linelayout.setVisibility(View.INVISIBLE);

}

else if(v == l2)

{

m.setBlock(24.0f);

linelayout.setVisibility(View.INVISIBLE);

}

else if(v == l3)

{

m.setBlock(36.0f);

linelayout.setVisibility(View.INVISIBLE);

}

}

}

mainfest

package="com.restore"

android:versionCode="1"

android:versionName="1.0" >

android:icon="@drawable/ic_launcher"

android:label="@string/app_name" >

android:name=".TestRestoreActivity"

android:label="@string/app_name" >

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值