为了实现在TextView中显示图片,有时对图片的宽度与高度有限制的话,可以对实现进行放大与缩小操作!

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
androidrientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView android:text="TextView01" android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<TextView android:text="TextView02" android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>

GridView.java

package a.gridview;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.TextView;

public class GridView extends Activity {

private TextView text;
private TextView text1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView) findViewById(R.id.TextView01);
Drawable draw = this.getResources().getDrawable(R.drawable.srvmng);
text.setCompoundDrawablesWithIntrinsicBounds(null, draw, null,null);
text.setText("应用");

text1 = (TextView) findViewById(R.id.TextView02);
Drawable draw1 = this.getResources().getDrawable(R.drawable.srvmng);
int w = draw1.getIntrinsicWidth();
int h = draw1.getIntrinsicHeight();
Rect rect = draw1.getBounds();
text1.setCompoundDrawablesWithIntrinsicBounds(null, zoomDrawable(draw1,32,32), null,null);
text1.setText("设置");
}

static Drawable zoomDrawable(Drawable drawable, int w, int h)
{
int width = drawable.getIntrinsicWidth();
int height= drawable.getIntrinsicHeight();
Bitmap oldbmp = drawableToBitmap(drawable); // drawable转换成bitmap
Matrix matrix = new Matrix(); // 创建操作图片用的Matrix对象
float scaleWidth = ((float)w / width); // 计算缩放比例
float scaleHeight = ((float)h / height);
matrix.postScale(scaleWidth, scaleHeight); // 设置缩放比例
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true); // 建立新的bitmap,其内容是对原bitmap的缩放后的图
return new BitmapDrawable(newbmp); // 把bitmap转换成drawable并返回
}

static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成bitmap
{
int width = drawable.getIntrinsicWidth(); // 取drawable的长宽
int height = drawable.getIntrinsicHeight();
Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565; // 取drawable的颜色格式
Bitmap bitmap = Bitmap.createBitmap(width, height, config); // 建立对应bitmap
Canvas canvas = new Canvas(bitmap); // 建立对应bitmap的画布
drawable.setBounds(0, 0, width, height);
drawable.draw(canvas); // 把drawable内容画到画布中
return bitmap;
}

}

更多相关文章

  1. Android(安卓)对话框(Dialog)大全 建立你自己的对话框 【转】
  2. android图像绘制(五)——画布保存为图片
  3. android让图片等比例缩放
  4. 设置activity背景图片
  5. Android缩放图片文件引起的OOM异常
  6. Android画布(cavas)
  7. android 图片放大缩小 多点缩放
  8. android浏览本地html
  9. [置顶] 学习链接

随机推荐

  1. Android的权限permission
  2. android在java代码中绘制矩形框
  3. Android(安卓)ListView(Selector 背景图片
  4. 第12天android:短信发送+测试使用
  5. Mac下获取android studio keystore的SHA1
  6. android 开发上传图片遇到返回 FileNotFo
  7. Android之获取手机信息
  8. android TextView的跑马灯效果的实现
  9. Android(安卓)通过继承TextView类自定义
  10. Android核心基础(五)