|
楼主 |
发表于 2012-3-23 11:50:04
|
显示全部楼层
加入了倒影效果的Bitmap的函数。代码如下
package com.util;
02
03
import android.graphics.Bitmap;
04
import android.graphics.Bitmap.Config;
05
import android.graphics.Canvas;
06
import android.graphics.Color;
07
import android.graphics.LinearGradient;
08
import android.graphics.Matrix;
09
import android.graphics.Paint;
10
import android.graphics.PorterDuff.Mode;
11
import android.graphics.PorterDuffXfermode;
12
import android.graphics.Shader.TileMode;
13
14
public class BitmapUtil {
15
16
public static Bitmap createTxtImage(String txt, int txtSize) {
17
Bitmap mbmpTest = Bitmap.createBitmap(txt.length() * txtSize + 4,
18
txtSize + 4, Config.ARGB_8888);
19
Canvas canvasTemp = new Canvas(mbmpTest);
20
Paint p = new Paint();
21
p.setAntiAlias(true);
22
p.setColor(Color.WHITE);
23
p.setTextSize(txtSize);
24
canvasTemp.drawText(txt, 2, txtSize - 2, p);
25
return mbmpTest;
26
}
27
28
public static Bitmap createReflectedImage(Bitmap originalImage) {
29
// The gap we want between the reflection and the original image
30
final int reflectionGap = 0;
31
32
int width = originalImage.getWidth();
33
int height = originalImage.getHeight();
34
35
// This will not scale but will flip on the Y axis
36
Matrix matrix = new Matrix();
37
matrix.preScale(1, -1);
38
39
// Create a Bitmap with the flip matrix applied to it.
40
// We only want the bottom half of the image
41
Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0,
42
height / 2, width, height / 2, matrix, false);
43
44
// Create a new bitmap with same width but taller to fit reflection
45
Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
46
(height + height / 2), Config.ARGB_8888);
47
48
// Create a new Canvas with the bitmap that's big enough for
49
// the image plus gap plus reflection
50
Canvas canvas = new Canvas(bitmapWithReflection);
51
// Draw in the original image
52
canvas.drawBitmap(originalImage, 0, 0, null);
53
// Draw in the gap
54
Paint defaultPaint = new Paint();
55
canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
56
// Draw in the reflection
57
canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
58
59
// Create a shader that is a linear gradient that covers the reflection
60
Paint paint = new Paint();
61
LinearGradient shader = new LinearGradient(0,
62
originalImage.getHeight(), 0, bitmapWithReflection.getHeight()
63
+ reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
64
// Set the paint to use this shader (linear gradient)
65
paint.setShader(shader);
66
// Set the Transfer mode to be porter duff and destination in
67
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
68
// Draw a rectangle using the paint with our linear gradient
69
canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
70
+ reflectionGap, paint);
71
72
return bitmapWithReflection;
73
}
74
}
复制代码
最后就是主界面代码中对Gallery的调用了,关键代码如下
private CustomGallery gallery;
02
private UniversityAdapter pAdapter;
03
private ArrayList<University> universityList = new ArrayList<University>();
04
private void init(){
05
pAdapter = new UniversityAdapter(MainActivity.this, universityList);//初始化我们自定义的Adapter
06
University p = new University();
07
p.setName("电子科技大学");
08
universityList .add(p);
09
University p1 = new University();
10
p1.setName("清华大学");
11
universityList .add(p1);
12
University p2 = new University();
13
p2.setName("北京大学");
14
universityList .add(p2);
15
gallery.setAdapter(pAdapter);//设置Gallery显示的内容
16
gallery.setSelection(Integer.MAX_VALUE / 2);//通过setSelection() 可以设置当前选中的元素,这里我们将
17
18
其设置在中间
19
gallery.setOnItemSelectedListener(this);//这里对Item项进行监听,以实现刷新显示的效果
20
}
21
22
@Override
23
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
24
long arg3) {
25
// TODO Auto-generated method stub
26
pAdapter.notifyDataSetChanged(arg2);//arg2会返回当前选中项的位置,调用此方法,通知更新
27
}
28
29
30
@Override
31
public void onNothingSelected(AdapterView<?> arg0) {
32
// TODO Auto-generated method stub
33
} |
|