2013-08-22 49 views
5

當第一次運行(或者一些重大更新後)時,某些Google應用程序或核心Android系統本身有時會顯示一個透明覆蓋圖,並提供一個簡短的工具提示如何使用新功能。一個例子可能是下面的圖像。第一次運行時顯示覆蓋工具提示

在Android框架中是否有一些用於創建這些API的API或者是否全部是自定義的?在後一種情況下如何實現它?最後但並非最不重要的是,這些工具提示是否有某種官方/技術名稱可供用戶參考(在Google上搜索關於該主題的某些信息時可能會有所幫助)?謝謝。

編輯

我已經獲得這說明更確切地明白我的意思的屏幕截圖。除了「指針」圖形之外,此工具提示在時鐘應用程序圖標周圍還突出顯示了一個圓圈。這不僅僅是點擊後消失的半透明覆蓋圖:此時鐘圖標可以被點擊甚至長時間按下,但是其他圖標(圓圈外)不會被訪問,直到toolip消失。所有這些行爲都是專門爲此目的而定製的,還是存在一些內置設施?

enter image description here

+1

我正要自己問這個問題,雖然我是在「氣球工具提示」效果之後。你有沒有找到一種方法來做到這一點?我不想讓我的佈局更復雜一些功能將被使用一次... – Basic

回答

3

這僅僅是一個半透明的對話框。

插入的themes.xml下面的代碼在res文件夾

<style name="Theme.TranparentDialog" parent="@android:style/Theme.NoTitleBar"> 
     <item name="android:colorBackgroundCacheHint">@null</item> 
     <item name="android:windowIsTranslucent">true</item> 
     <item name="android:colorForeground">@color/transparent</item> 
     <item name="android:windowIsFloating">false</item> 
     <item name="android:backgroundDimEnabled">false</item> 
     <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> 
     <item name="android:windowBackground">@color/transparent</item> 
    </style> 

創建屏幕上的對話框,獲得半透明的效果/

Dialog dialog= new Dialog(context, R.style.Theme_TutorialDialog); 

dialog.show();

+0

謝謝你的答案。不過,我提供了更精確的屏幕截圖並稍微更新了我的問題。請看看它。 – Natix

0

如果在上面的對話框中用作背景,此視圖可用於在屏幕上繪製透明矩形。

public class WindowView extends View { 
private static final int LOW_DPI_STATUS_BAR_HEIGHT = 19; 

private static final int MEDIUM_DPI_STATUS_BAR_HEIGHT = 25; 

private static final int HIGH_DPI_STATUS_BAR_HEIGHT = 38; 
Context mContext; 

int mHeight, mWidth, mTop, mRight, mLeft, mBottom; 
Rect mLeftRect, mRightRect, mTopRect, mBottomRect; 

Rect mRect; 
Paint mPaint; 
View mAnchorView; 
Paint mTempPaint1; 

public WindowView(Context context, AttributeSet attrs) { 
    this(context, attrs, 0); 
} 

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

public WindowView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    mContext = context; 
    initVariables(); 
} 

boolean mReset = false; 

GestureDetector mGestureDetector; 
GestureDetector.SimpleOnGestureListener mSimpleOnGestureListener; 

private View getView() { 
    return this; 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if (mSimpleOnGestureListener == null) { 
     mSimpleOnGestureListener = new SimpleOnGestureListener() { 
      @Override 
      public boolean onSingleTapConfirmed(MotionEvent e) { 
       if (mListener != null && e.getX() >= mRect.left 
         && e.getX() <= mRect.right && e.getY() >= mRect.top 
         && e.getY() <= mRect.bottom) 
        mListener.onClick(getView()); 
       return true; 
      } 
     }; 
    } 
    if (mGestureDetector == null) { 
     mGestureDetector = new GestureDetector(mContext, 
       mSimpleOnGestureListener); 
    } 
    mGestureDetector.onTouchEvent(event); 
    return true; 
} 

private void initVariables() { 
    mPaint = new Paint(); 
    mPaint.setStyle(Style.FILL); 
    mPaint.setColor(mContext.getResources().getColor(
      R.color.tut_transcluscent_bg)); 
    mRect = new Rect(); 

    mTempPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG); 
    mTempPaint1.setAlpha(64); 
    mTempPaint1.setColorFilter(new PorterDuffColorFilter(Color.WHITE, 
      Mode.SRC_IN)); 
} 

OnClickListener mListener; 

public void setTransparentBackGroundColor(int color) { 
    mPaint.setColor(color); 
    invalidate(); 
} 

@Override 
public void setOnClickListener(OnClickListener l) { 
    mListener = l; 
} 

public View getAnchorView() { 
    return mAnchorView; 
} 

float[] mPaddings; 
Rect mAnchorRect;; 

public void setAnchorView(View mView, Rect rect, float[] paddings) { 
    mAnchorRect = rect; 
    if (mAnchorRect == null) 
     mAnchorView = mView; 
    else 
     mAnchorView = null; 
    mPaddings = paddings; 
    requestLayout(); 
    invalidate(); 
} 

@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    if (mAnchorView != null || mAnchorRect != null) 
     initView(); 
    mReset = true; 
} 

boolean mFlagInvalid = false; 
Handler mAnrHandler; 

public void setHandler(Handler handler) { 
    mAnrHandler = handler; 
} 

private int getStatusBarHeight() { 
    DisplayMetrics displayMetrics = new DisplayMetrics(); 
    ((WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE)) 
      .getDefaultDisplay().getMetrics(displayMetrics); 

    int statusBarHeight; 

    switch (displayMetrics.densityDpi) { 
    case DisplayMetrics.DENSITY_HIGH: 
     statusBarHeight = HIGH_DPI_STATUS_BAR_HEIGHT; 
     break; 
    case DisplayMetrics.DENSITY_MEDIUM: 
     statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT; 
     break; 
    case DisplayMetrics.DENSITY_LOW: 
     statusBarHeight = LOW_DPI_STATUS_BAR_HEIGHT; 
     break; 
    default: 
     statusBarHeight = MEDIUM_DPI_STATUS_BAR_HEIGHT; 
    } 
    return statusBarHeight; 
} 

public void initView() { 

    mFlagInvalid = false; 
    int top = 0, left = 0; 
    if (mAnchorView != null) { 
     top = mAnchorView.getTop(); 
     left = mAnchorView.getLeft(); 
     View temp = mAnchorView; 
     int cnt = 0; 
     try { 
      while (((View) temp.getParent()).getId() != android.R.id.content) { 
       temp = (View) temp.getParent(); 
       int scrolledHeight = 0; 
       if (temp instanceof ScrollView) { 
        scrolledHeight = ((ScrollView) temp).getScrollY(); 
       } 
       top = top + temp.getTop() - scrolledHeight; 
       left = left + temp.getLeft(); 
       cnt++; 
       if (cnt > 100) { 
        if (mAnrHandler != null) 
         mAnrHandler.sendEmptyMessage(9); 
        mFlagInvalid = true; 
        break; 
       } 
      } 
     } catch (Exception e) { 
      mFlagInvalid = true; 
      if (mAnrHandler != null) 
       mAnrHandler.sendEmptyMessage(8); 
      e.printStackTrace(); 
     } 
     TypedValue tv = new TypedValue(); 
     if (getContext() 
       .getTheme() 
       .resolveAttribute(
         VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB ? android.R.attr.actionBarSize 
           : R.attr.actionBarSize, tv, true)) { 
      int actionBarHeight = TypedValue.complexToDimensionPixelSize(
        tv.data, getResources().getDisplayMetrics()); 
      top = top + actionBarHeight; 
     } 

    } else if (mAnchorRect != null) { 
    } 

    if (mFlagInvalid) 
     init(20, 20, 50, 50); 
    else { 
     if (mAnchorRect != null) { 
      init(mAnchorRect.bottom, mAnchorRect.right, mAnchorRect.left, 
        mAnchorRect.top); 
     } else 
      init(mAnchorView.getHeight(), mAnchorView.getWidth(), left, top); 
    } 
} 

public void init(int height, int width, int left, int top) { 
    mWidth = width; 
    mHeight = height; 
    mTop = top; 
    mBottom = top + height; 
    mLeft = left; 
    mRight = left + width; 

    mLeft = (int) (mLeft + mPaddings[0] * mWidth); 
    mTop = (int) (mTop + mPaddings[1] * mHeight); 
    mRight = (int) (mRight - mPaddings[2] * mWidth); 
    mBottom = (int) (mBottom - mPaddings[3] * mHeight); 
    mRect = new Rect(mLeft, mTop, mRight, mBottom); 

    invalidate(); 
} 

public Rect getAnchorRect() { 
    return mRect; 
} 

@Override 
protected void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    initOnceOnDraw(); 
    canvas.drawRect(mLeftRect, mPaint); 
    canvas.drawRect(mTopRect, mPaint); 
    canvas.drawRect(mRightRect, mPaint); 
    canvas.drawRect(mBottomRect, mPaint); 
    canvas.drawRect(mRect, mTempPaint1); 
} 

private void initOnceOnDraw() { 
    if (mReset) { 
     mReset = false; 
     mTopRect = new Rect(0, 0, getWidth(), mRect.top); 
     mLeftRect = new Rect(0, mRect.top, mRect.left, mRect.bottom); 
     mRightRect = new Rect(mRect.right, mRect.top, getWidth(), 
       mRect.bottom); 
     mBottomRect = new Rect(0, mRect.bottom, getWidth(), getHeight()); 
    } 
}} 

的方法setAnchorView()給出了其覆蓋有要繪製的看法..

mWindowView = (WindowView) mTutorialView 
          .findViewById(R.id.windowview_tutorial); 
         mWindowView.setTransparentBackGroundColor(backgroundColor); 

        mWindowView.setAnchorView(view, null, padding); 

希望這有助於