在我的项目中,Android和iOS的工作有所不同。 includeNativeBool为假。

例如:


代码如下:

@Override
public void paint(Graphics g) {
    x = getX();
    y = getY();
    w = getWidth();
    h = getHeight();
    r1 = w/20;
    r2 = r1/2;
    d1 = r1*2;
    d2 = r2*2;

    // Fill background
    g.setColor(getStyle().getFgColor());
    g.fillRect(x, y, w, h);
}

public void FirstPart(Graphics g) {
    g.setColor(getStyle().getBgColor());
    // North-West
    g.fillArc(x-r1, y-r1, d1, d1, 270, 360);
    // North
    g.fillArc(x+w/2-r1, y-r1, d1, d1, 180, 360);
    // North-East
    g.fillArc(x+w-r1, y-r1, d1, d1, 90, 180);
}

public void MiddlePartBegin(Graphics g) {
    g.setColor(getStyle().getBgColor());
    // North-West
    g.fillArc(x-r2, y-r2, d2, d2, 270, 360);
    // North-East
    g.fillArc(x+w-r2, y-r2, d2, d2, 90, 180);
}

public void MiddlePartEnd(Graphics g) {
    if (dash != null) {
        int c = w/iW + (w % iW > 0 ? 1 : 0); // Ceil
        for (int i=0;i<c;i++) {
            g.drawImage(dash, i*iW+x, y+h-1);
        }
    }

    g.setColor(getStyle().getBgColor());
    // South-West
    g.fillArc(x-r2, y+h-r2, d2, d2, 270, 360);
    // South-East
    g.fillArc(x+w-r2, y+h-r2, d2, d2, 270, 360);
}

public void LastPart(Graphics g) {
    g.setColor(getStyle().getBgColor());
    // South-West
    g.fillArc(x-r1, y+h-r1, d1, d1, 270, 360);
    // South-East
    g.fillArc(x+w-r1, y+h-r1, d1, d1, 270, 360);
}


或这个:



作为图像,我使用了URLImage类。这是我的适配器代码:

public static final URLImage.ImageAdapter ToCircle = new URLImage.ImageAdapter() {

    int borderWidth = 6;

    public EncodedImage adaptImage(EncodedImage downloadedImage, EncodedImage placeholderImage) {
        Image originalImage;

        // Crop and resize
        int w = downloadedImage.getWidth();
        int h = downloadedImage.getHeight();
        if (w > h) {
            originalImage = downloadedImage.subImage(
                (w-h)/2, 0,
                h, h,
                true
            );
        } else {
            originalImage = downloadedImage.subImage(
                0, (h-w)/2,
                w, w,
                true
            );
        }


        int pS = Math.min(placeholderImage.getHeight(), placeholderImage.getWidth());
        originalImage = originalImage.scaledHeight(pS);


        w = originalImage.getWidth();
        h = originalImage.getHeight();
        Log.p(Integer.toString(w)+";"+Integer.toString(h));
        Image finalImage = Image.createImage(w+2*borderWidth, h+2*borderWidth);

        Image maskedImage = originalImage.applyMask(
            createCircleMask(w,h)
        );

        Graphics g  = finalImage.getGraphics();
        g.setColor(0xff3d00);
        g.fillRect(
                0, 0,
                finalImage.getWidth(), finalImage.getHeight()
               // 0, 360
        );
        g.drawImage(maskedImage, borderWidth, borderWidth);

        w = finalImage.getWidth();
        h = finalImage.getHeight();

        return EncodedImage.createFromImage(
            finalImage.applyMask(
                createCircleMask(w,h)
            ),
            false
        );
    }

    public Object createCircleMask(int w, int h) {
        Image maskImage = Image.createImage(w, h);
        Graphics g = maskImage.getGraphics();
        g.setAntiAliased(true);
        g.setColor(0x000000);
        g.fillRect(0, 0, w, h);
        g.setColor(0xffffff);
        g.fillArc(0, 0, w, h, 0, 360);
        return maskImage.createMask();
    }

    public boolean isAsyncAdapter() {
        return false;
    }
};


在最后的麻烦中,图像可能无法调整为plasehoder图像大小...

另外我还注意到具有透明性的元素显示为透明,值为0xFF

最佳答案

由于iOS在状态栏区域下绘制,因此顶部缺少填充是由于includeNativeBool=false造成的。

对于遮罩,请看这是否有帮助:

    originalImage = originalImage.scaledHeight(pS);

    // then add this
    originalImage = EncodedImage.createFromImage(originalImage, false);


关于没有出现的弧,很难告诉您如何将代码应用于UI。我假设您没有使用像玻璃一样的东西?

由于组件可以触发自己的重新绘制,因此某些自定义绘制代码可能无法以正确的顺序发生。

09-11 18:35