wird diese Szene machen
ich in Ihrem GameView Klasse einige neue Änderungen vorgenommen. Unten ist eine Kopie der neuen GameView-Klasse.
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);
}
}
es durch XML innerhalb Aktivität hinzugefügt wird.
<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>
Output
es durch den Code in der Aktivität Hinzufügen
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);
}
}
Ausgabe
EDIT
Wenn Sie die Parameter festlegen möchten, während es pragmatisch Zugabe dann initialiseView Verfahren wie
sein
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);
}
Ausgang:
'GameView gv = new GameView (this, null);' - Sie haben vergessen, 'new'. Und das muss innerhalb einer Klassenmethode sein, wenn es nicht schon ist. –