Created
November 26, 2014 14:29
-
-
Save nightbear1009/9e872a0fe7768b964bbf to your computer and use it in GitHub Desktop.
a splite image example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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