1. 程式人生 > >Android 分享介面製作從底部滑出動畫效果

Android 分享介面製作從底部滑出動畫效果

1.自定義類如下:

BaseDialog:

import android.app.Dialog;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;

public class BaseDialog extends Dialog {
    public View mCreateView;
    public Context mContext;
    public int mScreenWidth;//螢幕寬  
    public int mScreenHeight;//螢幕高  
    public int mDensity;//單位畫素  
    public Animation mExitAnim;//退出動畫  
    public Animation mEnterAnim;//進入動畫  

    public BaseDialog(Context context) {
        super(context);
        init(context);
    }

    private void init(Context context) {
        mContext = context;
        //計算螢幕的寬高畫素  
        DisplayMetrics metrics = mContext.getResources().getDisplayMetrics();
        mScreenHeight = metrics.heightPixels;
        mScreenWidth = metrics.widthPixels;
        mDensity = (int) metrics.density;
    }

    @Override
    public void show() {
        super.show();
        enterAnimation();//進入動畫  
    }

    /**
     *  
     *      * 進入動畫 
     *      
     */
    private void enterAnimation() {
        if (mEnterAnim == null) {
            mEnterAnim = new TranslateAnimation(1, 0, 1, 0, 1, 1, 1, 0);
            mEnterAnim.setDuration(500);
        }
        mCreateView.startAnimation(mEnterAnim);
    }

    /**
     *  
     *      * 退出動畫 
     *      
     */
    private void exitAnimation() {
        if (mExitAnim == null) {
            mExitAnim = new TranslateAnimation(1, 0, 1, 0, 1, 0, 1, 1);
            mExitAnim.setDuration(500);
            mExitAnim.setAnimationListener(
                    new AnimationListener() {
                        @Override
                        public void onAnimationStart(Animation animation) {
                        }

                        @Override
                        public void onAnimationRepeat(Animation animation) {
                        }

                        @Override
                        public void onAnimationEnd(Animation animation) {
                            dismissDialog(); //動畫完成執行關閉  
                        }
                    });
        }
        mCreateView.startAnimation(mExitAnim);
    }

    /**
     *  
     *      * 執行關閉 
     *      
     */


    private void dismissDialog() {
        super.dismiss();
    }

    /**
     *  
     *      * 執行動畫 
     *      
     */

    @Override

    public void dismiss() {
        exitAnimation();
    }

    @Override

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            dismiss();
        }
        return super.onKeyDown(keyCode, event);
    }

ShareDialog:

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.king.shenggaofei_galleryfinal.R;

/**
 * sgf
 * QQ分享底部彈出動畫
 */
public class ShareDialog extends BaseDialog{
    //介面顯示的資料
    private String[]  mTexts={"QQ好友","QQ空間","微信好友","微信朋友圈","微信收藏","新浪微博"};
    private int[]	  mImgs={R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,
            R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,
            R.drawable.ic_launcher_background};//圖片資料

    private OnClickListener mListener;

    public ShareDialog(Context context) {
        super(context);
        mCreateView=initView();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //設定Dialog沒有標題。需在setContentView之前設定,在之後設定會報錯
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        //設定Dialog背景透明效果,必須設定一個背景,否則會有系統的Dialog樣式:外部白框
        this.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        setContentView(mCreateView);//新增檢視佈局
        setLayout();
    }

    private View initView() {
        int num=mTexts.length;
        int number=3;//一行顯示的數量 //最外面的佈局
        LinearLayout group=new LinearLayout(mContext);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);

        group.setLayoutParams(params);
        group.setBackgroundColor(Color.BLACK);
        group.setOrientation(LinearLayout.VERTICAL);//設定垂直方向
        //標題
        TextView title=new TextView(mContext);
        params=new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                50*mDensity);
        params.bottomMargin=1;
        title.setLayoutParams(params);
        title.setText("分享");
        title.setTextColor(Color.BLACK);
        title.setTextSize(16);
        title.setGravity(Gravity.CENTER);//字居中
        title.setBackgroundColor(Color.WHITE);
        group.addView(title);
        //設定內容
        for(int i=0,j=0;i<num;){
            j++;
            LinearLayout ll=new LinearLayout(mContext);
            params=new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    0,1.0f);//設定權重
            ll.setLayoutParams(params);
            ll.setBackgroundColor(Color.WHITE);
            ll.setOrientation(LinearLayout.HORIZONTAL);//設定水平方向
            group.addView(ll);
            for(;i<j*number && i<num;i++){//一行三個
                //設定分享圖文控制元件
                TextView item=new TextView(mContext);
                params=new LinearLayout.LayoutParams(
                        mScreenWidth/number,
                        LinearLayout.LayoutParams.MATCH_PARENT
                );
                params.topMargin=10*mDensity;
                params.bottomMargin=10*mDensity;
                item.setLayoutParams(params);
                item.setBounds(50, 50);//設定圖片的寬高
                Drawable top = mContext.getResources().getDrawable(mImgs[i]);
                item.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);//給DrawableTop設定圖片
                item.setText(mTexts[i]);//設定文字
                item.setGravity(Gravity.CENTER);//字居中
                MyOnClickListener listener=new MyOnClickListener(i);
                item.setOnClickListener(listener);//設定點選監聽器
                ll.addView(item);
            }
        }
        //底部
        TextView cancel=new TextView(mContext);
        params=new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                50*mDensity);
        params.topMargin=1;
        cancel.setLayoutParams(params);
        cancel.setText("取消");
        cancel.setTextColor(Color.BLACK);
        cancel.setTextSize(16);
        cancel.setGravity(Gravity.CENTER);//字居中
        cancel.setBackgroundColor(Color.WHITE);
        cancel.setOnClickListener(new View.OnClickListener() {//設定取消
            public void onClick(View arg0) {
                ShareDialog.this.cancel();
            }
        });
        group.addView(cancel);
        return group;
    }
    public class MyOnClickListener implements android.view.View.OnClickListener{
        private int mPosition;
        public MyOnClickListener(int position){
            mPosition=position;
        }
        @Override
        public void onClick(View v) {
//            Toast.makeText(mContext, mTexts[mPosition], Toast.LENGTH_SHORT).show();
            ShareDialog.this.cancel();
            if(mListener!=null){
                mListener.OnClick(v,mPosition);//呼叫自定義介面, TODO(分享在呼叫介面實現分享功能)
            }
        }
    }
    public interface OnClickListener {
        void OnClick(View v,int position);
    }
    public void setOnClickListener(OnClickListener listener){
        mListener=listener;
    }
    private void setLayout(){
        WindowManager.LayoutParams params=this.getWindow().getAttributes();
        params.width=mScreenWidth;
        params.gravity=Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM;;//水平居中、底部
        this.getWindow().setAttributes(params);
    }

}

自定義TextView:

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;

public class TextView extends android.widget.TextView{
    private Context mContext;
    private int mWidth=30;
    private int mHeight=30;

    //儲存設定的圖片
    private Drawable mLeft,   mTop,   mRight,   mBottom;

    public TextView(Context context) {
        this(context,null);
    }

    public TextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext=context;
        init();
    }

    private void init() {
        float density=mContext.getResources().getDisplayMetrics().density;
        //將dip轉化成px
        mWidth*=density;
        mHeight*=density;
        //手動呼叫該方法,設定文字上下左右方向的圖片寬高
        setCompoundDrawablesWithIntrinsicBounds(mLeft, mTop, mRight, mBottom);//設定圖片的寬高
    }
    /**
     * 重寫該方法,文字上下左右方向的圖片大小設定
     * @param left
     * @param top
     * @param right
     * @param bottom
     */
    @Override
    public void setCompoundDrawablesWithIntrinsicBounds( Drawable left,  Drawable top,  Drawable right,  Drawable bottom) {
        if (left != null) {
            left.setBounds(0, 0, mWidth, mHeight);
            mLeft=left;
        }
        if (right != null) {
            right.setBounds(0, 0, mWidth, mHeight);
            mRight=right;
        }
        if (top != null) {
            top.setBounds(0, 0, mWidth, mHeight);
            mTop=top;
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, mWidth, mHeight);
            mBottom=bottom;
        }
        setCompoundDrawables(left, top, right, bottom);//設定圖片的寬高
    }
    /**
     * 設定圖片的寬高
     * @param width
     * @param height
     */
    public void setBounds(int width,int height){
        mWidth=width;
        mHeight=height;
        init();
    }

}

佈局的形式是使用程式碼自定義實現的,沒有佈局,下面看看如何呼叫和監聽:

  //分享
                ShareDialog shareDialog = new ShareDialog(MainActivity.this);
                shareDialog.show();
                shareDialog.setOnClickListener(new ShareDialog.OnClickListener() {
                    @Override
                    public void OnClick(View v, int position) {
                        if(position == 0){
                            Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT).show();
                        }else  if(position == 1){
                            Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT).show();
                        } else  if(position == 2){
                            Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT).show();
                        }else  if(position == 3){
                            Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT).show();
                        }else  if(position == 4){
                            Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT).show();
                        }else  if(position == 5){
                            Toast.makeText(MainActivity.this, position+"", Toast.LENGTH_SHORT).show();
                        }else  if(position == 6){

                        }
                    }
                });