Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Develop Tricks(一):視窗尺寸、任意定位與拖曳移動

編輯:關於 Android 程式設計

一、設定 AlertDialog 的大小

AlertDialog dialog = builder.setTitle("消息列表")
        .setView(layout)
        .create();
dialog.show();
// 設定視窗的大小
dialog.getWindow().setLayout(300, 200);

順序很關鍵:dialog.show() 一定要放在 dialog.getWindow().setLayout(300, 200) 之前,否則不生效。原因是 getWindow() 在 show() 之後才會真正綁定到 WindowManager,未顯示前調整無異於空作。

另一種寫法是直接操作屬性:

WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
params.width = 300;
params.height = 200;
dialog.getWindow().setAttributes(params);

其實 setLayout 就是對這個方法的封裝:

final WindowManager.LayoutParams attrs = getAttributes();
attrs.width = width;
attrs.height = height;
if (mCallback != null) {
    mCallback.onWindowAttributesChanged(attrs);
}

二、在任意位置顯示 View

透過 WindowManager 直接把 View 加上去即可:

mWindowManager = getWindowManager();
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
mDialogView = inflater.inflate(R.layout.dialogview, null);

WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width  = WindowManager.LayoutParams.WRAP_CONTENT;
params.y += 100;
params.x += -30;

initDialogComponents(mDialogView);
// 添加對話框
mWindowManager.addView(mDialogView, params);

三、任意 View 的拖曳移動通用做法

同樣是覆寫 onTouchEvent,透過 ACTION_DOWN 記錄起點、ACTION_MOVE 計算位移並重新 layout,同時夾住邊界避免拖出螢幕:

package ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.ScaleAnimation;
import android.widget.Button;

public class MenuButton extends Button {

    private Context mContext;
    private WindowManager mWm;
    int lastX;
    int lastY;
    int screenWidth;
    int screenHeight;

    public MenuButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mWm = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
        screenWidth  = dm.widthPixels;
        screenHeight = dm.heightPixels;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
        measure(0, 0);
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_MOVE:
                int dx = (int) event.getRawX() - lastX;
                int dy = (int) event.getRawY() - lastY;
                int left   = this.getLeft() + dx;
                int top    = this.getTop() + dy;
                int right  = this.getRight() + dx;
                int bottom = this.getBottom() + dy;

                if (left < 0) { left = 0; right = left + this.getWidth(); }
                if (right > screenWidth) { right = screenWidth; left = right - this.getWidth(); }
                if (top < 0) { top = 0; bottom = top + this.getHeight(); }
                if (bottom > screenHeight) { bottom = screenHeight; top = bottom - this.getHeight(); }

                this.layout(left, top, right, bottom);
                lastX = (int) event.getRawX();
                lastY = (int) event.getRawY();
                break;
            case MotionEvent.ACTION_UP:
                Animation alphaAnimation = new AlphaAnimation(0.5f, 1f);
                alphaAnimation.setDuration(1000);

                Animation scaleAnimation1 = new ScaleAnimation(1.0f, 1.2f, 1.0f, 1.2f);
                scaleAnimation1.setDuration(500);
                Animation scaleAnimation2 = new ScaleAnimation(1.0f, 0.5f, 1.0f, 0.5f);
                scaleAnimation2.setDuration(500);

                AnimationSet animationSet = new AnimationSet(true);
                animationSet.addAnimation(alphaAnimation);
                animationSet.addAnimation(scaleAnimation1);
                animationSet.addAnimation(scaleAnimation2);
                animationSet.setFillAfter(true);

                Animation currentAnima = getAnimation();
                if (currentAnima != null) {
                    currentAnima.cancel();
                    animationSet.reset();
                }
                startAnimation(animationSet);
                break;
        }
        return true;
    }
}

四、ImageView 設為 FIT_START 後去除多餘留白

imageView.setScaleType(ImageView.ScaleType.FIT_START);
imageView.setAdjustViewBounds(true);

兩行搭配使用即可:setScaleType 決定圖片對齊起始邊,setAdjustViewBounds 則讓 ImageView 依圖片實際比例收縮自身邊界,消除空白。順帶一提,取得螢幕尺寸時 dm.widthPixels 並不含狀態列與導覽列,若拖曳邊界需要更精確,建議搭配 getWindowManager().getDefaultDisplay().getMetrics() 並自行扣除系統 UI 高度。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved