2011-05-18 21 views
3

Android Controls這個控件在android中的名稱是如何實現的?

+1

,看起來像一些應用程序的窗口小部件,並可能不是一個單一的控制。 – jkhouw1 2011-05-18 11:26:47

+0

它是做什麼的?我甚至無法識別它。 – Blundell 2011-05-18 11:55:53

+1

我認爲它是在每個Android設備默認情況下。它會在用戶從一個屏幕滑動到另一個屏幕時自動顯示,以查看更多(已安裝應用程序的圖標)圖標。 – Sourav 2011-05-18 12:30:04

回答

2

這是非標準控制。

我已經實現了它。我已經用簡單的方法完成了,只需要在ImageView上畫出所需的全部內容。

我的代碼看起來像這樣。它可以進一步改進(例如移動大小或xml佈局中的顏色定義),但是您可以從中獲得創意。並沒有考慮到設備屏幕的不同尺寸和密度。

/** 
* Padding between circles displaying position 
*/ 
private static final int PADDING = 20; 
/** 
* Radius of circles displaying position 
*/ 
private static final int RADIUS = 4; 
// Color of the selected element's indicator 
private int activeColor; 
// Color of the not selected element's indicator 
private int inactiveColor; 

// Call this in View constructor or in activity onCreate() 
Resources res = getResources(); 
activeColor = res.getColor(R.color.sliderActiveColor); 
inactiveColor = res.getColor(R.color.sliderInactiveColor); 

/** 
* Draws position of current element. 
* Call it on fling events. 
* @param currentPosition Current element position 
*/ 
protected void drawPosition(int currentPosition) { 
    // Control height 
    final int height = 20; 
    // Control width 
    final int bmpWidth = 200; 

    // Count of all elements 
    int count = viewFlipper.getChildCount(); 

    // Calculate first element position on canvas 
    float initialPosition = bmpWidth/2 - (count - 1) * (PADDING/2 + RADIUS); 
    final int shift = PADDING + 2 * RADIUS; 

    Bitmap bitmap = Bitmap.createBitmap(bmpWidth, height, Config.ARGB_8888); 

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
    paint.setStyle(Style.FILL); 
    Canvas c = new Canvas(bitmap); 

    for (int i = 0; i < count; i++) { 
     // Draw circle for each element, current element with different color 
     if (i == currentPosition) { 
      paint.setColor(activeColor); 
     } else { 
      paint.setColor(inactiveColor); 
     } 
     c.drawCircle(initialPosition + i * shift, PADDING/2, RADIUS, paint); 
    } 

    // Draw on ImageView 
    sliderImage.setImageBitmap(bitmap); 
} 

而結果:

Fling indicator