Android华容道之一步一步实现-3 -手指触摸处理

华容道关键点之一出现了,就是处理触摸,包括手指按下,抬起,移动等。

自己实现一个处理触摸的类,然后处理

onTouchEvent(MotionEvent event)

事件,在这里处理手指按下,抬起,移动事件。

代码为:

这里的效果是手指触摸一下,16块小图像依次交换

package com.example.szhrd;

 

import android.R.bool;

import android.app.Activity;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Matrix;

import android.graphics.Paint;

import android.os.Bundle;

import android.view.Menu;

import android.view.MenuItem;

import android.view.MotionEvent;

import android.view.View;

 

public class MainActivity extends Activity {

 int bit_num = 16;

 Bitmap ori_bitmap = null;

    

 img_puzzle puzzles[] = new img_puzzle[bit_num];

 int bitmap_width;

 int bitmap_height;

 int offset_x = 100;

 int offset_y = 100;

 

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

 @Override

 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);

  ori_bitmap = BitmapFactory.decodeResource(getResources(),

    R.drawable.koala);

  int width = ori_bitmap.getWidth();

  int height = ori_bitmap.getHeight();

  int new_width = width / 4;

  int new_height = height / 4;

  Matrix matrix = new Matrix();

  matrix.postScale(0.5f, 0.5f);

  bitmap_width = new_width / 2;

  bitmap_height = new_height / 2;

  for (int k = 0; k < bit_num; k++) {

   puzzles[k] = new img_puzzle();

  }

  for (int i = 0; i < 4; i++) {

   for (int j = 0; j < 4; j++) {

    int pos = i * 4 + j;

    int x = offset_x + j * bitmap_width;

    int y = offset_y + i * bitmap_height;

 

    if (pos == bit_num - 1) {

     puzzles[pos].bitmap = Bitmap.createBitmap(new_width, new_height, Config.ARGB_8888);

     puzzles[pos].is_blank = true;

 

    } else {

     puzzles[pos].bitmap = Bitmap.createBitmap(ori_bitmap,

       new_width * j, new_height * i, new_width,

       new_height, matrix, true);

    }

    puzzles[pos].x = puzzles[pos].ori_x = x;

    puzzles[pos].y = puzzles[pos].ori_y = y;

 

   }

  }

  this.setContentView(new DragView(this));

  // setContentView(new DragView(this));

 }

 

 @Override

 protected void onDestroy() {

  super.onDestroy();

  for (int i = 0; i < bit_num; i++) {

   if (null != puzzles[i].bitmap) {

 

    puzzles[i].bitmap.recycle();

    puzzles[i].bitmap = null;

   }

  }

 }

 

 private class img_puzzle {

  int x;

  int y;

  int ori_x;

  int ori_y;

  Bitmap bitmap = null;

  boolean is_blank = false;

 }

 

 protected void switch_pos() {

  int x0 = puzzles[0].x;

  int y0 = puzzles[0].y;

  for(int i=1;i<bit_num ;i++)

  {

   int x = puzzles[i].x;

   int y=puzzles[i].y;

   puzzles[i-1].x = x;

   puzzles[i-1].y = y;   

  }

  puzzles[15].x = x0;

  puzzles[15].y = y0;

 }

 

 private class DragView extends View {

  private Paint paint;

 

  public DragView(Context context) {

   super(context);

   paint = new Paint();

  }

 

  @Override

  public void draw(Canvas canvas) {

   super.draw(canvas);

   // canvas.drawColor(Color.RED);

   paint.setStrokeWidth((float) 5.0);

 

   for (int i = 0; i < 4; i++) {

    int x = offset_x;

    int y = offset_y + bitmap_height * i;

 

    for (int j = 0; j < 4; j++) {

     int pos = i * 4 + j;

     Bitmap bitmap = puzzles[pos].bitmap;

     // int x_1 = offset_x + j*bitmap.getWidth();

     // int y_1 = offset_y+i * bitmap.getHeight();

     int x_1 = puzzles[pos].x;

     int y_1 = puzzles[pos].y;

     canvas.drawBitmap(bitmap, x_1, y_1, paint);

     canvas.drawLine(x_1, y_1, x_1, y_1 + bitmap_height, paint);

    }

    canvas.drawLine(x, y, x + bitmap_width * 4, y, paint);

 

   }

  }

 

  @Override

  public boolean onTouchEvent(MotionEvent event) {

   super.onTouchEvent(event);

   switch (event.getAction()) {

   case MotionEvent.ACTION_DOWN:

    break;

   case MotionEvent.ACTION_UP:

    switch_pos();

    invalidate();

    break;

   case MotionEvent.ACTION_MOVE:

    

    

    return true;

   default:

 

   }

   return true;

  }

 }

}

 

下一步

Android华容道之一步一步实现-4-图像块移动算法

发布了77 篇原创文章 · 获赞 5 · 访问量 11万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览