背景圖設置和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"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM