編輯:Android開發實例
首先看看效果:
///項目布局
//// attrs.xml 自定義屬性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SlidingText">
<attr name="animationDuration" format="integer" />
</declare-styleable>
</resources>
///// 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"
xmlns:slidingtext="http://schemas.android.com/apk/res/com.testSildingTextView"
android:layout_height="fill_parent">
<com.testSildingTextView.SlidingTextView
android:id="@+id/sliding_textview" android:layout_width="fill_parent"
android:layout_height="wrap_content"
slidingtext:animationDuration="500"
android:layout_gravity="center">
<TextView android:layout_width="fill_parent" android:gravity="center_horizontal"
android:layout_height="wrap_content" android:text="sssssss" />
</com.testSildingTextView.SlidingTextView>
</LinearLayout>
首先自定義名稱xmlns:slidingtext=http://schemas.android.com/apk/res/com.testSildingTextView
這裡使用了自定義的名稱
slidingtext:animationDuration="500"
///// SlidingTextView
private String[] showTexts = new String[] { "ssssss", "aaaaaa", "bbbbbb" };
// 用來記錄顯示哪個字符串
private int count = 0;
private int mDuration;
private TextView text;
private int textWidth = 200;
//獲取自定義變量
public SlidingTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SlidingText);
mDuration = a
.getInteger(R.styleable.SlidingText_animationDuration, 750);
}
//設置要顯示的字符串
public void setShowText(String[] showTexts){
this.showTexts=showTexts;
}
// 回調函數 界面初始化快結束時調用
protected void onFinishInflate() {
super.onFinishInflate();
text = (TextView) this.getChildAt(0);
mHandler.postDelayed(appear, 1000);
}
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// 1為出現,2為隱藏
switch (msg.arg1) {
case 1:
doAnimationOpen();
break;
case 2:
doAnimationClose();
break;
}
}
};
public void doAnimationOpen() {
post(appear);
}
// 出現的效果
Runnable appear = new Runnable() {
public void run() {
TranslateAnimation animation;
int fromXDelta = 0, toXDelta = 0, fromYDelta = 0, toYDelta = 0;
int calculatedDuration = 0;
fromXDelta = textWidth;
toXDelta = 0;
calculatedDuration = mDuration * Math.abs(toXDelta - fromXDelta)
/ textWidth;
animation = new TranslateAnimation(fromXDelta, toXDelta,
fromYDelta, toYDelta);
animation.setDuration(calculatedDuration);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
if(showTexts.length!=0){
count = (count + 1) % showTexts.length;
text.setText(showTexts[count]);
}
text.setVisibility(VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mHandler.postDelayed(hide, 2500);
}
});
startAnimation(animation);
}
};
public void doAnimationClose() {
post(hide);
}
// 隱藏的效果
Runnable hide = new Runnable() {
public void run() {
TranslateAnimation animation;
int fromXDelta = 0, toXDelta = 0, fromYDelta = 0, toYDelta = 0;
int calculatedDuration = 0;
toXDelta = -1 * textWidth;
calculatedDuration = mDuration * Math.abs(toXDelta - fromXDelta)
/ textWidth;
animation = new TranslateAnimation(fromXDelta, toXDelta,
fromYDelta, toYDelta);
animation.setDuration(calculatedDuration);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
//動畫結束時 設置textview的狀態
@Override
public void onAnimationEnd(Animation animation) {
mHandler.postDelayed(appear, 500);
text.setVisibility(INVISIBLE);
}
});
startAnimation(animation);
}
};
這個動畫效果主要是每次開出一條線程來運行的,首次運行後等2點5秒,就將textview以動畫效果向左運行,等0.5秒後從右邊出現,動畫結束後隱藏,不斷循環
代碼下載
testSildingTextView.zip
登錄應用程序的屏幕,詢問憑據登錄到一些特定的應用。可能需要登錄到Facebook,微博等本章介紹了,如何創建一個登錄界面,以及如何管理安全問題和錯誤嘗試。首先,必須定義兩
繪制圓環其實很簡單,有大概以下三種思路. 這裡先說網上提到的一種方法。思路是先繪制內圓,然後繪制圓環(圓環的寬度就是paint設置的paint.setStroke
一般來說在Android裡要實現樹形菜單,都是用ExpandableList(也有高手自己繼承ListView或者LinearLayout來做),但是Expand
由於項目需要,我們需要一個可以橫向滾動的,又可以豎向滾動的 表格。而且又要考慮大數據量(行)的展示視圖。經過幾天的研究終於搞定,做了一個演示。貼圖如下: &nbs