5、裁剪的一个小用处
[java] view
plain
Activity
{
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
}
private Bitmap mBitmap;
private int limitLength
= 0;
private boolean status =
HIDE;//显示还是隐藏的状态,最开始为HIDE
public SampleView(Context context)
{
super(context);
mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.image1);
protected void onDraw(Canvas
canvas) {
while (i * CLIP_HEIGHT <=
heigth) {//计算clip的区域
, i * CLIP_HEIGHT,
limitLength, (i + 1) *
CLIP_HEIGHT));
region.union(new Rect(width -
limitLength, i * CLIP_HEIGHT, width, (i +
1)
if (status == HIDE)
{//如果此时是隐藏
status=SHOW;
if(limitLength>=width)
public class ClippingRegion extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private class SampleView extends View {
private Bitmap mBitmap;
private int limitLength = 0;
private int width;
private int heigth;
private static final int CLIP_HEIGHT = 30;
private boolean status = HIDE;//显示还是隐藏的状态,最开始为HIDE
private static final boolean SHOW = true;//显示图片
private static final boolean HIDE = false;//隐藏图片
public SampleView(Context context) {
super(context);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image1);
limitLength = width = mBitmap.getWidth();
heigth = mBitmap.getHeight();
}
@Override
protected void onDraw(Canvas canvas) {
Region region = new Region();
int i = 0;
while (i * CLIP_HEIGHT <= heigth) {//计算clip的区域
if (i % 2 == 0) {
region.union(new Rect(0, i * CLIP_HEIGHT, limitLength, (i + 1) * CLIP_HEIGHT));
} else {
region.union(new Rect(width - limitLength, i * CLIP_HEIGHT, width, (i + 1)
* CLIP_HEIGHT));
}
i++;
}
canvas.clipRegion(region);
canvas.drawBitmap(mBitmap, 0, 0, new Paint());
if (status == HIDE) {//如果此时是隐藏
limitLength -= 5;
if(limitLength<=0)
status=SHOW;
} else {//如果此时是显示
limitLength += 5;
if(limitLength>=width)
status=HIDE;
}
invalidate();
}
}
}
效果就是一直这样交叉的隐藏和显示图片
转自