为了实现在TextView中显示图片,有时对图片的宽度与高度有限制的话,可以对实现进行放大与缩小操作!
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="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(http://www.my400800.cn )
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;
}
}
显示效果:
给 TextView里面的内容添加链接
之前使用Java开发桌面应用的时候,在JTextPane中添加超链接并且设置监听是个很麻烦的事情,最终我也没找到一个很好的方法。用来开发android就爽了,API封装的不错,添加个超链接变的非常简单。
首先,在TextView所属xml配置文件中,直接添加android:autoLink特性即可,它支持一个或多个(用分割)自定义的值:none、web、email、phone或all。
另外,你还可以用Linkify来添加超链接,下面介绍一下这个类:
Linkify是一个辅助类,通过RegEx样式匹配,自动地在TextView类(和继承的类)中创建超链接。
符合特定的RegEx样式的文本会被转变成可点击的超链接,这些超链接隐式地调用startActivity(new Intent(Intent.ACTION_VIEW, uri)),符合的文本会作为目标URI。
你可以指定任意的字符串样式为链接;方便地,Linkify类提供了预置的通用内容类型(如电话号码和e-mail、web地址)。
Linkify.addLinks静态方法接受一个View来制作链接,还包括一个或多个支持的默认内容类型的位结果。Linkify类提供了一些内容类型:WEB_URLS、EMAIL_ADDRESSES、PHONE_NUMBERS和ALL.
接下来的代码片段显示如何为TextView制作链接显示web和e-mail地址为超链接。当点击时,它们会相应地打开浏览器或e-mail应用程序。
- TextView textView = (TextView)findViewById(R.id.myTextView);
- Linkify.addLinks(textView, Linkify.WEB_URLSLinkify.EMAIL_ADDRESSES);
可是有时候我们需要自定义一些超链接,像新浪微博中的@和#,这时候怎么办呢?
为 了定义自己的链接字符串,你需要创建一个RegEx样式来匹配文本,进而显示成超链接。和本地类型一样,通过调用Linkify.addLinks来指定 目标View,但这次,传入的是的RegEx样式。你还可以传入一个前缀,当链接点击时,它会添加到目标URI上。例如:
- int flags = Pattern.CASE_INSENSITIVE;
- Pattern p = Pattern.compile(“\\bquake[-9]*\\b”, flags);
- Linkify.addLinks(myTextView, p, “content://com.paad.earthquake/earthquakes/”);