【问题标题】:Make an custom view and extends it from other activities制作自定义视图并从其他活动中扩展它
【发布时间】:2014-09-28 09:34:19
【问题描述】:

我有一个游戏,每个关卡都有一个回收过程,但随着时间的推移会越来越难, 我可以查看共同特征并通过每个级别的活动来扩展它吗?

有人有解决办法吗?或其他想法? (我需要一个单独的活动中的每个级别)

这就是我做的......

public class Prev extends Activity{
private Bitmap image1;
private Bitmap image2;
private Bitmap image3;
private Bitmap image4;

private final int IMAGE_1_X = 50;
private final int IMAGE_2_X = 450;
private final int IMAGE_3_X = 50;
private final int IMAGE_4_X = 450;

private final int IMAGE_1_Y = 50;
private final int IMAGE_2_Y = 50;
private final int IMAGE_3_Y = 300;
private final int IMAGE_4_Y = 300;


@Override
    protected void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        Preview p = new Preview(this);
        setContentView(p);
}
public void setImages(Bitmap image1, Bitmap image2, Bitmap image3, Bitmap image4)
{
    this.image1 = image1;
    this.image2 = image2;
    this.image3 = image3;
    this.image4 = image4;
}
public void setRawsNames(String sound_1, String sound_2, String sound_3, String sound_4)
{
    this.sound_1 = sound_1;
    this.sound_2 = sound_2;
    this.sound_3 = sound_3;
    this.sound_4 = sound_4;
}
 public class Preview extends SurfaceView{

public Preview(Context context)
{
    super(context);
}
@Override
public void onDraw(Canvas canvas)
{
    canvas.drawBitmap(image1, IMAGE_1_X, IMAGE_1_Y,null);
    canvas.drawBitmap(image2, IMAGE_2_X, IMAGE_2_Y,null);
    canvas.drawBitmap(image3, IMAGE_3_X, IMAGE_3_Y,null);
    canvas.drawBitmap(image4, IMAGE_4_X, IMAGE_4_Y,null);
}
}

}

这里我将扩展上层阶级

public class MainActivity extends Prev {
private Bitmap image1;
private Bitmap image2;
private Bitmap image3;
private Bitmap image4;

public MainActivity() 
{  
    preperImages();
    setImages(image1, image2, image3, image4);
}
private void preperImages()
{
    image1 = BitmapFactory.decodeResource(getResources(), R.drawable.one);
    image2 = BitmapFactory.decodeResource(getResources(), R.drawable.two);
    image3 = BitmapFactory.decodeResource(getResources(), R.drawable.three);
    image4 = BitmapFactory.decodeResource(getResources(), R.drawable.four);

}

}

【问题讨论】:

    标签: android view android-custom-view


    【解决方案1】:

    不确定您到底想要实现什么。据我了解,您想重用视图。为此,您可以在布局中使用include 标签。

    【讨论】:

      猜你喜欢
      • 2013-04-10
      • 1970-01-01
      • 2011-12-06
      • 1970-01-01
      • 2017-12-04
      • 2016-12-27
      • 2023-02-02
      • 1970-01-01
      • 2014-04-03
      相关资源
      最近更新 更多