gpt4 book ai didi

java - 代号一圆角 URLImage 不显示

转载 作者:行者123 更新时间:2023-11-30 07:11:40 27 4
gpt4 key购买 nike

我正在尝试使用 URLImage.createToStorage 从 url 中提取图像。不过,我希望该图片显示为圆形,因此我向图像添加了蒙版。但是,当我运行时,标签仅显示占位符图像,而不显示网址图像。当我注释掉将圆形蒙版添加到图像中的代码时,图像将显示。我的圆形图像代码有问题吗?我使用了 Display.getInstance().callSerially()。

//Where I display the image. 
public void setUpProfile(Form f) {
Label imageLabel = findMyImage(f);
Image img = getImageFromRes("myprofile.png");
Image scaled = img.scaledWidth(f.getWidth() / 2);
EncodedImage enc = EncodedImage.createFromImage(scaled, false);

Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
imageLabel.setIcon(getRoundedImage(URLImage.createToStorage(enc,
"profileImage8", me.getPicture(), URLImage.RESIZE_SCALE_TO_FILL)));
f.revalidate();
}
});

findProfNameLabel(f).setText(me.getName());
findProfAgeLabel(f).setText(me.getAge() + " Years old");
findProfPrefLabel(f).setText("Interested in " + me.getPref());
}

public Image getRoundedImage(Image img) {

int w = img.getWidth();
int h = img.getHeight();

Image maskImage = Image.createImage(w, h);
Graphics g = maskImage.getGraphics();
g.setColor(0xffffff);
g.fillArc(0, 0, w, h, 0, 360);

Object mask = maskImage.createMask();
Image ret = img.applyMask(mask);
return ret;
}

在Form的beforeShow中调用setUpProfile()方法。

编辑:我在使用 URLImage.createMaskAdapter 的工作 setUpProfile() 方法中进行了编辑。并获得圆形图像。

public void setUpProfile(Form f) {
Label imageLabel = findMyImage(f);
Image mask = getImageFromRes("rounded-mask.png");
Image placeholder = getImageFromRes("myprofile.png").scaled(mask.getWidth(), mask.getHeight());
EncodedImage enc = EncodedImage.createFromImage(placeholder.applyMask(mask.createMask()),
false);
System.out.println("SetUpProfile picture " + me.getPicture());

imageLabel.setIcon(URLImage.createToStorage(enc, "profileImage8",
me.getPicture(), URLImage.createMaskAdapter(mask)));

findProfNameLabel(f).setText(me.getName());
findProfAgeLabel(f).setText(me.getAge() + " Years old");
findProfPrefLabel(f).setText("Interested in " + me.getPref());
}

最佳答案

您可以通过创建自定义ImageAdapter来实现此目的,该自定义ImageAdapter会在下载图像时自动为您生成round-mask

public static final URLImage.ImageAdapter RESIZE_SCALE_WITH_ROUND_MASK = new URLImage.ImageAdapter() {
@Override
public EncodedImage adaptImage(EncodedImage downloadedImage, EncodedImage placeholderImage) {
Image tmp = downloadedImage.scaledLargerRatio(placeholderImage.getWidth(), placeholderImage.getHeight());
if (tmp.getWidth() > placeholderImage.getWidth()) {
int diff = tmp.getWidth() - placeholderImage.getWidth();
int x = diff / 2;
tmp = tmp.subImage(x, 0, placeholderImage.getWidth(), placeholderImage.getHeight(), true);
} else if (tmp.getHeight() > placeholderImage.getHeight()) {
int diff = tmp.getHeight() - placeholderImage.getHeight();
int y = diff / 2;
tmp = tmp.subImage(0, y, Math.min(placeholderImage.getWidth(), tmp.getWidth()),
Math.min(placeholderImage.getHeight(), tmp.getHeight()), true);
}
Image roundMask = Image.createImage(tmp.getWidth(), tmp.getHeight(), 0xff000000);
Graphics gr = roundMask.getGraphics();
gr.setColor(0xffffff);
gr.fillArc(0, 0, tmp.getWidth(), tmp.getHeight(), 0, 360);
Object mask = roundMask.createMask();
tmp = tmp.applyMask(mask);
return EncodedImage.createFromImage(tmp, false);
}

@Override
public boolean isAsyncAdapter() {
return true;
}
};

然后这样应用它:

public void setUpProfile(Form f) {
Label imageLabel = findMyImage(f);
Image img = getImageFromRes("myprofile.png");
Image scaled = img.scaledWidth(f.getWidth() / 2);
EncodedImage enc = EncodedImage.createFromImage(scaled, false);

Display.getInstance().callSerially(new Runnable() {
@Override
public void run() {
imageLabel.setIcon(URLImage.createToStorage(enc,
"profileImage8", me.getPicture(), RESIZE_SCALE_WITH_ROUND_MASK));
f.revalidate();
}
});

findProfNameLabel(f).setText(me.getName());
findProfAgeLabel(f).setText(me.getAge() + " Years old");
findProfPrefLabel(f).setText("Interested in " + me.getPref());
}

关于java - 代号一圆角 URLImage 不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39127298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com