在Android中以编程方式创建自定义视图

时间:2017-11-01 03:12:19

标签: android

当我尝试创建GameView时,出现错误“预期函数调用”。

在我的主要活动中:

GameView gv = GameView(this, null);

public class GameView extends View {
        private Drawable mCustomImage;

        public GameView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mCustomImage = context.getResources().getDrawable(R.drawable.bg);
        }

        protected void onDraw(Canvas canvas) {
            Rect imageBounds = canvas.getClipBounds();  // Adjust this for where you want it

            mCustomImage.setBounds(imageBounds);
            mCustomImage.draw(canvas);
        }
    }
}

如何动态创建?

我正在尝试为我的活动添加视图。然后,GameView将以编程方式控制其子视图的位置。

3 个答案:

答案 0 :(得分:1)

这将使场景

  

我在你的GameView课程中做了一些新的改动。下面是新GameView类的副本。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;

public class GameView extends View {

private Context mContext;

private Drawable mCustomImage;


public GameView(Context context) {
    super(context);
    mContext = context;
    mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}

public GameView(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    mContext = context;
    mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}

public GameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    mContext = context;
    mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}

public GameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    mContext = context;
    mCustomImage = ContextCompat.getDrawable(context, R.mipmap.ic_launcher);
}

@Override
protected void onDraw(Canvas canvas) {
    Rect imageBounds = canvas.getClipBounds();
    mCustomImage.setBounds(imageBounds);
    mCustomImage.draw(canvas);
}
}
  

通过xml内部活动添加它。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <GameView
            android:layout_width="50dp"
            android:layout_height="50dp" />

    </LinearLayout>

</ScrollView>

  

输出

Image_One

  

通过活动中的代码添加

public class CustomActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_draw_line);


    initialiseView();

}

private void initialiseView() {

    LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);

    GameView mGameView = new GameView(this);

    mLinearLayout.addView(mGameView);

}
}
  

输出

Image_Two

  

修改

     

如果要在实际添加参数时设置参数,则initialiseView方法将类似于

private void initialiseView() {

    LinearLayout mLinearLayout = (LinearLayout) findViewById(R.id.linearLayout);

    GameView mGameView = new GameView(this);

    ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(50, 50);

    mGameView.setLayoutParams(layoutParams);

    mLinearLayout.addView(mGameView);

}
  

输出:

Image_Three

答案 1 :(得分:0)

GameView是一个类,而不是方法,因此您必须调用new才能使用它们,

试试这个:

GameView gv = new GameView(this, null);

public class GameView extends View {
        private Drawable mCustomImage;

        public GameView(Context context, AttributeSet attrs) {
            super(context, attrs);
            mCustomImage = context.getResources().getDrawable(R.drawable.bg);
        }
         ....

答案 2 :(得分:0)

使用活动new GameView(this, null);内的onCreate来创建视图的新实例。

然后使用方法setContentView(View)在您的活动中使用它。

public class MainActivity extends AppCompatActivity{

    private GameView gameView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gameView = new GameView(this, null);
        setContentView(gameView);
        //other code
    }
    //other methods
}