2017-11-01 2 views
0

Wenn ich versuche, meine GameView zu erstellen, erhalte ich einen Fehler "Funktionsaufruf erwartet".Erstellen Sie eine benutzerdefinierte Ansicht programmatisch in Android

in meiner Haupttätigkeit:

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); 
     } 
    } 
} 

Wie erstelle ich diese dynamisch?

Ich versuche, eine Sicht auf meine Aktivität hinzuzufügen. Die GameView wird dann die Position ihrer Untersichten programmgesteuert steuern.

+2

'GameView gv = new GameView (this, null);' - Sie haben vergessen, 'new'. Und das muss innerhalb einer Klassenmethode sein, wenn es nicht schon ist. –

Antwort

1

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

Image_One

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

Image_Two

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:

Image_Three

+0

In der onCreate, wenn dies meine MainActivity ist, sollte es 'setContentView (R.layout.activity_main);'? Du erwähnst "draw line", aber ich habe das nirgendwo sonst gesehen – quantumpotato

+0

Ich versuche das Layout, bekomme Fehler "Element GameView ist hier nicht erlaubt" – quantumpotato

+0

Hallo Abhishek, also wurde Ihre Aktivität "ActivityDrawLine" genannt? Ich habe diese Zeile aus XML entfernt und jetzt kann ich zeichnen! – quantumpotato

0

GameView ist eine Klasse, keine Methode, so müssen Sie new sie zu verwenden, rufen Sie diese

versuchen:

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); 
     } 
     .... 
0

Verwenden new GameView(this, null); innerhalb onCreate Ihrer Tätigkeit eine neue Instanz Ihrer erstellen Aussicht.

Verwenden Sie dann die Methode setContentView(View), um sie in Ihrer Aktivität zu verwenden.

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 
} 
Verwandte Themen