加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Android自定义类似于QQ的消息提示框

发布时间:2020-12-15 03:21:57 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 效果还不错,整体上是Translate动画和FrameLayout布局的结合,下面看一下代码: activiy_main.xml LinearLayout xmlns:android="http://schemas.androi

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

效果还不错,整体上是Translate动画和FrameLayout布局的结合,下面看一下代码:

activiy_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >

    <FrameLayout
       android:layout_width="match_parent"
       android:layout_height="wrap_content" >

        <TextView
           android:id="@+id/toast"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_gravity="center"
           android:layout_marginBottom="20dp"
           android:layout_marginTop="40dp"
           android:background="@color/transparent_black"
           android:gravity="center"
           android:paddingBottom="5dp"
           android:paddingLeft="40dp"
           android:paddingRight="40dp"
           android:paddingTop="5dp"
           android:text="@string/toast"
           android:textColor="@color/white"
           android:textSize="16sp"
           android:visibility="invisible" />

        <TextView
           android:id="@+id/title"
           android:layout_width="match_parent"
           android:layout_height="wrap_content"
           android:background="@color/blue"
           android:gravity="center"
           android:padding="5dp"
           android:text="@string/title"
           android:textColor="@color/white"
           android:textSize="20sp" />
    </FrameLayout>

    <TextView
       android:id="@+id/info"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="10dp"
       android:gravity="center"
       android:text="@string/hello_world" />

    <Button
       android:id="@+id/btn"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:layout_marginTop="30dp"
       android:gravity="center"
       android:text="@string/btn" >
    </Button>

</LinearLayout>

MainActivity.java

 package cn.androiddevelop;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.TextView;
import cn.androiddevelop.test.R;

public class MainActivity extends Activity {
    TextView rootView;
    Button btn;
    Handler handler;
    TextView tv;
    Animation mTranslateInAnimation,mTranslateOutAnimation;
    boolean flag = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rootView = (TextView) findViewById(R.id.info);
        btn = (Button) findViewById(R.id.btn);
        tv = (TextView) findViewById(R.id.toast);

        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                tv.startAnimation(mTranslateOutAnimation);
                tv.setGravity(View.INVISIBLE);
                super.handleMessage(msg);
            }

        };

        // 定义进入与退出动画
        mTranslateInAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_SELF,-1.5f,0);
        mTranslateInAnimation.setDuration(1000);

        mTranslateOutAnimation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF,0f,-1.5f);
        mTranslateOutAnimation.setDuration(1000);

        mTranslateOutAnimation // 动画显示结束后将tv控件隐藏
                .setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        tv.setVisibility(View.INVISIBLE);
                        flag = true; // 恢复响应
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });

        btn.setOnClickListener(new Button.OnClickListener() {

            @Override
            public void onClick(View v) {
                if (flag) { // 第一次点击按钮生效,在消息框退出前不响应点击
                    flag = false;
                } else {
                    return;
                }

                // 显示消息框
                tv.startAnimation(mTranslateInAnimation);
                tv.setVisibility(View.VISIBLE);

                new Thread() {
                    @Override
                    public void run() {
                        try {
                            sleep(3000);
                            handler.sendEmptyMessage(0);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }.start();
            }
        });
    }
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读