Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
a splite image example
package com.fanpage.tedliang.shaderexample;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.ImageView;
public class SpliteImageView extends ImageView {
private float originY;
private float deltaY;
Bitmap rightBottom;
Bitmap leftup;
Bitmap leftBottom;
Bitmap rightup;
public SpliteImageView(Context context) {
super(context);
}
public SpliteImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public SpliteImageView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onTouchEvent(MotionEvent motionEvent) {
int action = motionEvent.getAction();
if (action == MotionEvent.ACTION_DOWN) {
originY = motionEvent.getY();
} else if (action == MotionEvent.ACTION_MOVE) {
deltaY = Math.abs(originY - motionEvent.getY());
} else if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
deltaY = 0;
originY = 0;
}
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
if (rightBottom == null) {
rightBottom = createRightDown();
leftup = createLeftUp();
leftBottom = createLeftDown();
rightup = createRightUp();
}
canvas.drawBitmap(leftup, -deltaY, -deltaY, null);
canvas.drawBitmap(rightBottom, deltaY, deltaY, null);
canvas.drawBitmap(leftBottom, -deltaY, deltaY, null);
canvas.drawBitmap(rightup, deltaY, -deltaY, null);
}
private Bitmap createRightUp() {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(bitmap);
myCanvas.clipRect(new Rect(getWidth() / 2, 0, getWidth(), getHeight() / 2));
super.onDraw(myCanvas);
return bitmap;
}
private Bitmap createLeftDown() {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(bitmap);
myCanvas.clipRect(new Rect(0, getHeight() / 2, getWidth() / 2, getHeight()));
super.onDraw(myCanvas);
return bitmap;
}
private Bitmap createRightDown() {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(bitmap);
myCanvas.clipRect(new Rect(getWidth() / 2, getHeight() / 2, getWidth(), getHeight()));
super.onDraw(myCanvas);
return bitmap;
}
private Bitmap createLeftUp() {
Bitmap bitmap = Bitmap.createBitmap(getWidth() / 2, getHeight() / 2, Bitmap.Config.ARGB_8888);
Canvas myCanvas = new Canvas(bitmap);
super.onDraw(myCanvas);
return bitmap;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment