背景图设置和textView设置大集合

参考网址:http://blog.sina.com.cn/s/blog_5f1fe33f0100k9al.html
        http://www.ucrobotics.com/index.php/zh/forum/18-Android技术探讨/118-通过ScrollView控制元素滚动效果以及背景图片平铺实现
        http://android.tgbus.com/Android/tutorial/201104/350358.shtml
        http://trinea.iteye.com/blog/1143934

Android 图片平铺效果
我们大家都看过平铺的效果,那么我们都是怎么样才能实现的那,我们其实主要用到的就是api,我们一开始new一个bitmap,就可以了,但是,大家都没有想过,我们还可以用什么方法来做这个事情那,那么我们就来说说第二种方法,那就在用到了xml,上面我们说了两个方法,但android是非常强大的,也就是说我们还有第三个方法,那就是我们自己画出来,那么我们就来看看代码吧:

1)第一种利用系统提供的api实现

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
//bitmap = Bitmap.createBitmap(100, 20, Config.ARGB_8888);
BitmapDrawable drawable = new BitmapDrawable(bitmap);
drawable.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT );
drawable.setDither(true);
view.setBackgroundDrawable(drawable);

 2)第二种我们使用xml来轻松实现

< bitmap xmlns:android="http://schemas.android.com/apk/res/android" android:src="@drawable/img"
android:tileMode="repeat" />

3)第三种自己画出来

public static Bitmap createRepeater(int width, Bitmap src){
int count = (width + src.getWidth() - 1) / src.getWidth();

Bitmap bitmap = Bitmap.createBitmap(width, src.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);

for(int idx = 0; idx < count; ++ idx){
canvas.drawBitmap(src, idx * src.getWidth(), 0, null);
}

return bitmap;
}

4)在 Android 使用配合螢幕尺寸縮放的背景圖(9-Patch)
     http://cw1057.blogspot.com/2011/11/android-9-patch.html
     Draw 9-patch是android sdk裡面內建的一支程式
     http://blog.kenyang.net/2010/05/draw-9-patch.html

======================================
Android 图片渐变效果

首先建立文件drawable/shape.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <gradient android:startColor="#FFFFFFFF" android:endColor="#FFFF0000"
            android:angle="270"/>
</shape>

在该文件中设置渐变的开始颜色(startColor)、结束颜色(endColor)和角度(angle)

接着创建一个主题values/style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="NewTheme" parent="android:Theme">
<item name="android:background">@drawable/shape</item>
</style>
</resources>

然后在AndroidManifest.xml文件中的application或activity中引入该主题,如:

<activity android:name=".ShapeDemo" android:theme="@style/NewTheme">

========================================
通过ScrollView控制元素滚动效果

Android系统本身的很多应用都是具有滑动效果的,当用手上划或下划操作时,到达应用的边界后还会出现一段缓冲,显得很自然,同时滚动的速度也比较快!
如果联系人列表,短信列表,还有很多配置画面都有这个属性。

通过ListView肯定是可以实现滑动效果的,但实现起来比较复杂;

通过ScrollView却能很简单实现这一效果:

比如有个TextView,里面有很多内容;如果再其外面再套上一层<ScrollView>,浏览内容时就很方便了,可快速的定位到内容的尾部。

注意,<ScrollView>的直接子元素只能有一个,但子元素可以包含自己的子元素的。

还有就是默认<ScrollView>的子元素不是占满整个区域的,即使设置了android:layout_height="fill_parent"也不行;需要自己给ScrollView对象指定一个属性:

scrollview.setFillViewport(true);

这样就会让其子元素充满整个区域了。

关于ScrollView的中文API:www.cnblogs.com/over140/archive/2011/01/27/1945964.html
一个很不错的ListView示例:www.iteye.com/topic/540423

=====================================
设置背景颜色

1、设置背景色,集成自View的属性,xml中设置为

android:background="#A4A4A4"

java程序中

textView.setBackgroundColor(android.graphics.Color.RED);

注意上面setBackgroundColor参数必须为android.graphics.Color,而对于字符串的颜色值可以如下方式parseColor

textView.setBackgroundColor(android.graphics.Color.parseColor("#A4A4A4"));

======================================
textView文字中添加链接

String content = "<a href=\"http://www.1688.com\">alibaba</a>";  
textView.setText(Html.fromHtml(content));  
textView.setMovementMethod(LinkMovementMethod.getInstance());

=======================================
text部分文字样式修改

对于TextView控件,经常将其中不同的文字显示不同的样式,如下:

1、设置text和样式

statusText.setText(statusInfo, TextView.BufferType.SPANNABLE);

表示可以修改文字的样式,无TextView.BufferType.SPANNABLE会出现异常

2、修改样式

Spannable sp = (Spannable)textView.getText();
String text = textView.getText().toString();
sp.setSpan(new ForegroundColorSpan(Color.BLUE), 0, 10, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

上述表示将第1到第10个字符显示为蓝色,当然text需要先保证长度不小于10

========================================
设置文字居中

android:gravity="center"

设置文字垂直居中并水平向右

android:gravity="center_vertical|right"
posted @ 2012-05-16 18:29  日光之下无新事  阅读(4403)  评论(0编辑  收藏  举报