教材第四章习题1、2

习题一

MainActivity.java

import android.app.Activity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;


public class MainActivity extends Activity {
    TestView tView = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tView = (TestView)findViewById(R.id.t1);
        tView.setOnTouchListener(new mOnTouch());

    }

    class mOnTouch implements View.OnTouchListener {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int x1, y1;


            x1 = (int) event.getX();
            y1 = (int) event.getY();



            if ( x1<= 65 && x1 >= 11 && y1 <=70 && y1 >= 15){
               MainActivity.this.finish();

            }
            tView.getXY(x1, y1);
            tView.invalidate();
            return true;


        }
    }
}

activity_main.xml



    

TestView.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

class TestView extends View {
    int x=150,y=50;
    public TestView(Context context, AttributeSet attrs)
    {  super(context, attrs);  }
    void getXY(int _x, int _y)
    {
        x = _x;
        y = _y;
    }
    @Override
    protected void onDraw(Canvas canvas)
    {
        super.onDraw(canvas);
        canvas.drawColor(Color.CYAN); /*设置背景为青色*/
        Paint paint=new Paint();
        paint.setAntiAlias(true); /*去锯齿*/
        paint.setColor(Color.BLACK); /*设置paint的颜色*/
        canvas.drawCircle(x, y, 30, paint); /*画一个实心圆*/
        paint.setColor(Color.WHITE);/*画一个实心圆上的小白点*/
        canvas.drawCircle(x - 9, y - 9, 6, paint);

        paint.setColor(Color.RED);
        paint.setStrokeWidth(5);
                /*画空心矩形(正方形)*/
        canvas.drawRect(10, 10, 70, 70, paint);
    }

}

习题二

MainActivity.java

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
    public void onCreate(Bundle savedInstanceState)
           {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
              handWrite = (HandWrite)findViewById(R.id.handwriteview);
             clear = (Button)findViewById(R.id.clear);
             clear.setOnClickListener(new mClick());
            }
           private class mClick implements OnClickListener
    {
              public void onClick(View v)
               {
                  handWrite.clear();
               }
            }
       }

activity_main.xml


      
          
         
            

HandWrite.java

import android.content.Context;
   import android.graphics.*;
   import android.graphics.Paint.Style;
   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.cy).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(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);
            }
          }

你可能感兴趣的:(教材第四章习题1、2)