场景:制造TextView的倒影
制作TextView的倒影
package com.javaeye.graphics;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class BitmapReflect extends Activity
{
private static final String TAG = "***BitmapFactoryOptionsTest***";
LinearLayout ly;
TextView name;
TextView phone;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
name = new TextView(this);
phone = new TextView(this);
name.setText("bruce zhang");
phone.setText("13739188962");
ly = new LinearLayout(this);
ly.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
ly.setLayoutParams(llp);
ly.addView(name);
ly.addView(phone);
Button button = new Button(this);
button.setText("OK");
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
int width = phone.getWidth();
int height = name.getHeight() + phone.getHeight();
Log.e(TAG, "width: " + width + " height: " + height);
Bitmap.Config bc = Bitmap.Config.ARGB_8888;
Bitmap bitmap = Bitmap.createBitmap(width, height, bc);
Canvas canvas = new Canvas(bitmap);
ly.draw(canvas);
Bitmap bitmap2 = createReflectedImage(bitmap);
ImageView image = new ImageView(BitmapReflect.this);
image.setBackgroundDrawable(new BitmapDrawable(bitmap2));
ly.addView(image);
ly.postInvalidate();
}
});
ly.addView(button);
setContentView(ly);
}
public static Bitmap createReflectedImage(Bitmap originalImage) {
final int reflectionGap = 4; //倒影和原图片间的距离
int width = originalImage.getWidth();
int height = originalImage.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(1, -1);
//倒影部分
Bitmap reflectionImage = Bitmap.createBitmap(originalImage,
0, height / 2, width, height / 2, matrix, false);
//要返回的倒影图片
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
(height + height / 2), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmapWithReflection);
//画原来的图片
canvas.drawBitmap(originalImage, 0, 0, null);
Paint defaultPaint = new Paint();
//倒影和原图片间的距离
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
//画倒影部分
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, originalImage.getHeight(),
0, bitmapWithReflection.getHeight() + reflectionGap,
0x70ffffff, 0x00ffffff,
TileMode.MIRROR);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
return bitmapWithReflection;
}
}