45fan.com - 路饭网

搜索: 您的位置主页 > 手机频道 > 阅读资讯:移动开发例题4_5

移动开发例题4_5

2019-04-10 07:55:34 来源:www.45fan.com 【
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_height="match_parent"
  android:layout_width="match_parent"
  android:orientation="vertical"
    >

  <com.example.ex4_5.HandWrite
    android:id="@+id/hand"
    android:layout_height="280dp"
    android:layout_width="match_parent"
    />

  <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    >
    <Button
      android:id="@+id/clear"
      android:layout_width="200dp"
      android:layout_height="wrap_content"
      android:text="清屏"
      />
  </LinearLayout>


</LinearLayout>
package com.example.ex4_5;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends Activity {
  private HandWrite handWrite=null;
  private Button clear =null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    handWrite=(HandWrite)findViewById(R.id.hand);
    clear=(Button)findViewById(R.id.clear);
    clear.setOnClickListener(new mClick());
  }

  public class mClick implements OnClickListener{
    public void onClick(View v){
      handWrite.clear();
    }

  }
}
package com.example.ex4_5;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class HandWrite extends View {
  Paint paint=null;
  Bitmap originalBitmap=null;
  Bitmap new1_Bitmap=null;
  Bitmap new2_Bitmap=null;
  float startX=0,startY=0;
  float clickX=0,clickY=0;
  boolean isMove=true;
  boolean isClear=false;
  int color= Color.GREEN;
  float strokeWidth=2.0f;

  public HandWrite(Context context, AttributeSet attrs){
    super(context,attrs);
    originalBitmap= BitmapFactory.decodeResource(getResources(),R.drawable.icon2).copy(Bitmap.Config.ARGB_8888,true);
    new1_Bitmap=Bitmap.createBitmap(originalBitmap);
  }
  public void clear(){
    isClear=true;
    new2_Bitmap=Bitmap.createBitmap(originalBitmap);
    invalidate();
  }
  public void setstyle(float strokeWidth){
    this.strokeWidth=strokeWidth;
  }
  @Override
  protected void onDraw(Canvas canvas){
    super.onDraw(canvas);
    canvas.drawBitmap(HandWriting(new1_Bitmap),0,0,null);
  }
  public Bitmap HandWriting(Bitmap o_Bitmap){
    Canvas canvas=null;
    if(isClear){
      canvas=new Canvas(new2_Bitmap);
    }else {
      canvas=new Canvas(o_Bitmap);
    }
    paint=new Paint();
    paint.setStyle(Paint.Style.STROKE);
    paint.setAntiAlias(true);
    paint.setColor(color);
    paint.setStrokeWidth(strokeWidth);
    if(isMove){
      canvas.drawLine(startX,startY,clickX,clickY,paint);
    }
    startX=clickX;
    startY=clickY;
    if(isClear){
      return new2_Bitmap;
    }
    return o_Bitmap;
  }
  @Override
  public boolean onTouchEvent(MotionEvent event){
   clickX=event.getX();
   clickY=event.getY();
   if(event.getAction()==MotionEvent.ACTION_DOWN){
     isMove=false;
     invalidate();
     return true;
   }else if(event.getAction()==MotionEvent.ACTION_MOVE){
     isMove=true;
     invalidate();
     return true;

   }
   return super.onTouchEvent(event);

  }
}

 

 
 

本文地址:http://www.45fan.com/a/luyou/100332.html
Tags: 开发 移动 例题
编辑:路饭网
  • 上一篇:前端笔记06-jquery
  • 下一篇:没有了
  • 关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部