main.xml
Java代码
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent"
6 >
7 <TextView
8 android:layout_width="fill_parent"
9 android:layout_height="wrap_content"
10 android:text="@string/hello"
11 />
12 <TextView android:text="TextView01" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
13 <TextView android:text="TextView02" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
14 </LinearLayout>
GridView.java
Java代码
15 package a.gridview;
16
17 import android.app.Activity;
18 import android.graphics.Bitmap;
19 import android.graphics.Canvas;
20 import android.graphics.Matrix;
21 import android.graphics.PixelFormat;
22 import android.graphics.Rect;
23 import android.graphics.drawable.BitmapDrawable;
24 import android.graphics.drawable.Drawable;
25 import android.os.Bundle;
26 import android.widget.TextView;
27
28 public class GridView extends Activity {
29
30 private TextView text;
31 private TextView text1;
32 /** Called when the activity is first created. */
33 @Override
34 public void onCreate(Bundle savedInstanceState) {
35 super.onCreate(savedInstanceState);
36 setContentView(R.layout.main);
37 text = (TextView) findViewById(R.id.TextView01);
38 Drawable draw = this.getResources().getDrawable(R.drawable.srvmng);
39 text.setCompoundDrawablesWithIntrinsicBounds(null, draw, null,null);
40 text.setText("应用");
41
42 text1 = (TextView) findViewById(R.id.TextView02);
43 Drawable draw1 = this.getResources().getDrawable(R.drawable.srvmng);
44 int w = draw1.getIntrinsicWidth();
45 int h = draw1.getIntrinsicHeight();
46 Rect rect = draw1.getBounds();
47 text1.setCompoundDrawablesWithIntrinsicBounds(null, zoomDrawable(draw1,32,32), null,null);
48 text1.setText("设置");
49 }
50
51 static Drawable zoomDrawable(Drawable drawable, int w, int h)
52 {
53 int width = drawable.getIntrinsicWidth();
54 int height= drawable.getIntrinsicHeight();
55 Bitmap oldbmp = drawableToBitmap(drawable); // drawable转换成bitmap
56 Matrix matrix = new Matrix(); // 创建操作图片用的Matrix对象
57 float scaleWidth = ((float)w / width); // 计算缩放比例
58 float scaleHeight = ((float)h / height);
59 matrix.postScale(scaleWidth, scaleHeight); // 设置缩放比例
60 Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true); // 建立新的bitmap,其内容是对原bitmap的缩放后的图
61 return new BitmapDrawable(newbmp); // 把bitmap转换成drawable并返回
62 }
63
64 static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap
65 {
66 int width = drawable.getIntrinsicWidth(); // 取drawable的长宽
67 int height = drawable.getIntrinsicHeight();
68 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565; // 取drawable的颜色格式
69 Bitmap bitmap = Bitmap.createBitmap(width, height, config); // 建立对应bitmap
70 Canvas canvas = new Canvas(bitmap); // 建立对应bitmap的画布
71 drawable.setBounds(0, 0, width, height);
72 drawable.draw(canvas); // 把drawable内容画到画布中
73 return bitmap;
74 }
75
76 }
显示效果: