android中这个控件的名称是什么以及如何实现它?

时间:2011-05-18 11:23:00

标签: android controls

Android Controls

1 个答案:

答案 0 :(得分: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