産品經理用的是ios手機,于是android就走上了模仿的道路.做這個東西也走了一些彎路,寫一篇部落格放在這裡,以後自己也可用參考,也友善别人學習。
彎路:
1.剛開始本來用popupwindow去實作,做着之後發現如果用popupwindow實作的話,從标題欄下面彈出就比較麻煩.
2.最外層的布局本來是用linearlayout去實作的,然後标題欄跟彈出的那個textview外邊包裹一層relativelayout,這樣就會有一個問題,父布局relativelayout高度就是标題欄高度,提示框的高度最多也是标題欄高度,就算往下移動我們也看不到.
3.最外層布局改成relativelayout之後顯示循序也是有秩序的,第一個是内容view,第二個是提示的view,第三個是标題欄的view. android繪制view的原理就是最新畫上去的一定在最上面,我們要保證标題欄始終顯示,提示框可以遮住内容,内容是最後面的,是以需要第一個繪制。
實作步驟:
1.把顯示提示view封裝成自定義控件,繼承自textview.提供兩個公共方法供外部調用.封裝view的目的是代碼複用,如果在其他地方也需要這種顯示效果,在布局檔案中引用自定義
view即可
1).showtips 顯示提示view,調用向下移動動畫,移動完成後延時一秒,再向上移動,并且改變透明度,動畫結束後隐藏view。
2).settitleheight 看名字就知道設定标題欄的高度,因為我們這邊是從标題欄下面彈出,是以我們得計算标題欄的高度是多少,才能知道往下面移動多少合适.移動上去同理.
2.activity中顯示自定義view.調用自定義view的公共方法.
1).在activity中注冊回調接口來擷取标題欄的高度,然後指派給自定義view,
2).當我們需要提示的時候調用自定view的公共方法就行.
效果圖如下:
viewtestactivity.java 主activity,程式的入口.
/**
*
* @author ansen
* @create time 2015-10-20
*/
public class viewtestactivity extends activity implements ongloballayoutlistener{
private tiptextview tvtips;//提示
private textview tvtitle;//标題
@override
protected void oncreate(bundle savedinstancestate) {
super.oncreate(savedinstancestate);
setcontentview(r.layout.activity_test);
tvtips=(tiptextview) findviewbyid(r.id.tv_tips);
tvtitle=(textview) findviewbyid(r.id.tv_title);
//tvtitle在一個視圖樹中的焦點狀态發生改變時,注冊回調接口來擷取标題欄的高度
viewtreeobserver vto = tvtitle.getviewtreeobserver();
vto.addongloballayoutlistener(this);
findviewbyid(r.id.btn_show_tip).setonclicklistener(clicklistener);
}
private onclicklistener clicklistener=new onclicklistener() {
@override
public void onclick(view v) {
switch (v.getid()) {
case r.id.btn_show_tip://顯示提示
tvtips.showtips();
break;
}
}
};
@suppresslint("newapi")
public void ongloballayout() {
tvtitle.getviewtreeobserver().removeongloballayoutlistener(this);//删除監聽
tvtips.settitleheight(tvtitle.getheight());//把标題欄的高度指派給自定義的textview
}
activity_test.xml 顯示的布局檔案
<?xml version="1.0" encoding="utf-8"?>
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<view
android:layout_above="@+id/btn_show_tip"
android:layout_below="@+id/tv_title"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#00ffff"/>
<com.example.tips.view.tiptextview
android:id="@+id/tv_tips"
android:layout_height="wrap_content"
android:background="#faf3a1"
android:gravity="center"
android:paddingbottom="5dp"
android:paddingtop="5dp"
android:textcolor="#ff4e00"
android:text="用動畫實作提示"
android:visibility="gone"/>
<textview
android:id="@+id/tv_title"
android:layout_height="40dp"
android:text="這是一個标題欄"
android:background="#ff00ff"
android:textsize="18sp"/>
<button
android:id="@+id/btn_show_tip"
android:layout_alignparentbottom="true"
android:text="彈窗提示!!!"/>
</relativelayout>
tiptextview.java 自定義view,繼承textview
* 自定義textview 顯示提示資訊,顯示時有動畫效果(從上面彈出,然後改變透明度慢慢隐藏)
public class tiptextview extends textview{
private static final int start_time=400;//動畫顯示時間
private static final int end_time=400;//動畫移出時間
private static final int show_time=1000;//動畫顯示時間
private int titleheight=100;//标題欄預設的高度設定成100
public tiptextview(context context) {
super(context);
public tiptextview(context context, attributeset paramattributeset) {
super(context, paramattributeset);
public tiptextview(context context, attributeset paramattributeset,int paramint) {
super(context, paramattributeset, paramint);
public void showtips(){
setvisibility(view.visible);
//向下移動動畫
translateanimation downtranslateanimation=new translateanimation(0,0,0,titleheight);
downtranslateanimation.setduration(start_time);
downtranslateanimation.setfillafter(true);
startanimation(downtranslateanimation);
//動畫監聽
downtranslateanimation.setanimationlistener(new animationlistener() {
@override
public void onanimationstart(animation animation) {}
public void onanimationend(animation animation){//向下移動動畫結束
toptranslateanimation();
public void onanimationrepeat(animation animation) {}
});
private void toptranslateanimation(){
new handler().postdelayed(new runnable() {//延時1秒之後再向上移動
public void run(){
//向上移動動畫
translateanimation toptranslateanimation=new translateanimation(0,0,titleheight,0);
toptranslateanimation.setduration(end_time);
toptranslateanimation.setfillafter(true);
//改變透明度
alphaanimation alphaanimation=new alphaanimation(1,0);
alphaanimation.setduration(end_time);
//兩個動畫添加到動畫集合中
animationset animationset=new animationset(true);
animationset.addanimation(toptranslateanimation);
animationset.addanimation(alphaanimation);
startanimation(animationset);//開啟動畫
animationset.setanimationlistener(new animationlistener() {
@override
public void onanimationstart(animation animation) {}
public void onanimationrepeat(animation animation) {}
public void onanimationend(animation animation){//動畫結束隐藏提示的textview
setvisibility(view.gone);
}
});
},show_time);
/**
* 設定标題欄高度
* @param titleheight
*/
public void settitleheight(int titleheight) {
this.titleheight = titleheight;
以上有步驟說明,效果圖,源碼,相信大家都能看懂....推薦下自己建立的android qq群:202928390 歡迎大家的加入.
<a target="_blank" href="http://download.csdn.net/detail/lowprofile_coding/9197443">點選下載下傳源碼</a>