【问题标题】:Android: best practice for building a 2D avatar customizationAndroid:构建 2D 头像自定义的最佳实践
【发布时间】:2014-08-20 20:23:44
【问题描述】:

我正在尝试在我的 Android 应用中创建 2D 头像定制器而不使用游戏引擎

我基本上会以 PNG 图像作为开始的基础。然后我想在基础之上叠加其他图像来自定义角色。

创建这个的最佳选择是什么?我已经创建了一个自定义ImageView 并覆盖了onDraw()

public class AvatarView extends ImageView {

public AvatarView(Context context) {
    super(context);
}

public AvatarView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public AvatarView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.battle_run_char), 0, 70, null);
    canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.red_cartoon_hat), 70, 0, null);
}

}

通过使用坐标,这似乎非常具体。有没有更好的方法来实现这一点而不必使用坐标?

编辑:

这是我目前所拥有的。这家伙是与红帽子不同的图像。

【问题讨论】:

  • @Rod_Algonquin 已编辑原始帖子。这是我到目前为止所拥有的。在我投入更多精力和时间之前,只是想看看这是否是最好的方法。也在看这个应用程序:androidify.com,但不确定代码有多广泛。

标签: android android-custom-view custom-view avatar


【解决方案1】:

如果您打算动态添加 ImageView,那么如果不指定像素轴,就无法放置这些图像

您的自定义类中的一个问题是永远不要在您的 onDraw 方法中使用 decodeResource,因为它会被多次调用,这将导致 big lag problem,而是在您的 AvatarView 中创建一个 init 方法并解码它,并在所有构造函数中调用该 init 方法。

样本:

public class AvatarView extends ImageView {

private Bitmap body;
private Bitmap hat;
public AvatarView(Context context) {
    super(context);
    init();
}

public AvatarView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public AvatarView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init()
{
  body = getResources(), R.drawable.battle_run_char);
  hat = getResources(), R.drawable.red_cartoon_hat);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(BitmapFactory.decodeResource(body , 0, 70, null);
    canvas.drawBitmap(BitmapFactory.decodeResource(hat , 70, 0, null);
}

}

【讨论】:

  • 适当注意解码资源。您会建议(在您看来)这将是化身定制的一个不错的选择吗?那就是使用坐标进行自定义视图。仅仅为了创建头像而添加游戏引擎似乎有点矫枉过正吧??
  • @TheNomad 我不会,记住所有屏幕设备都有不同的像素数,所以当你在更高像素数的设备上尝试这个时,它不会像上图那样。
  • @TheNomad 我建议为每个自定义使用 1 个图像。但这需要很多图片。
  • 嗯,为每个自定义设置 1 个图像似乎是一项 hack 工作。最终我们会有很多不同的组合:帽子、衬衫、裤子、手套等等,会有太多的组合。我将不得不考虑其他选择。并感谢有关其他密度大小的信息。将研究 SVG 选项。
猜你喜欢
  • 2014-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-20
  • 2014-02-15
  • 1970-01-01
相关资源
最近更新 更多