I am new to android and I am a bit stuck. I am trying to create a simple drawing app which shows an example on the top of the page and a square space for it in below it. Aim is to display a letter and a kid needs to practice in replicating it.
I have troubles including the drawing class in the layout which needs to restrict its boundaries.
this is the main layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/exampleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
enter code here` android:id="#+id/drawingBoard"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/exampleText">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
this is the PaintView class
import android.app.ActionBar;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
public class PaintView extends View {
public ViewGroup.LayoutParams param;
private Path path = new Path();
private Paint brush = new Paint();
public PaintView(MainActivity context) {
super(context);
brush.setAntiAlias(true);
brush.setColor(Color.RED);
brush.setStyle(Paint.Style.STROKE);
brush.setStrokeJoin(Paint.Join.ROUND);
brush.setStrokeWidth(8f);
param = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX,pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX,pointY);
break;
default:
return false;
}
postInvalidate();
return false;
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path,brush);
}
this is how it is called in the Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PaintView paintView = new PaintView(this);
setContentView(paintView);
}
I need to fit the drawing board into the "drawingBoard".
It is not necessary the right approach, but this is as far as I got.
Thanks in advance
Attach the customview to a fragment.
This is a completely different approach so you will have to make a lot of changes to your code.
The other approach would be adding the view programmatically to the ConstraintLayout.
In MainActivity,
protected void onCreate(Bundle savedInstanceStatee) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//remove those lines.
/*
PaintView paintView = new PaintView(this);
setContentView(paintView);*/
ConstraintLayout parentLayout = (ConstraintLayout)findViewById(R.id.drawingBoard);
ConstraintSet set = new ConstraintSet();
PaintView paintView = new PaintView(this);
// set view id, else getId() returns -1
paintView.setId(View.generateViewId());
layout.addView(paintView, 0);
set.clone(parentLayout);
// connect start and end point of views, in this
case top of child to top of parent.
set.connect(paintView.getId(),
ConstraintSet.TOP,parentLayout.getId(),
ConstraintSet.TOP, 60);
// ... similarly add other constraints
set.applyTo(parentLayout);
}
In the activity you are overwriting the content view with just the paint view:
setContentView(paintView);
Remove that line.
Add the PaintView in XML instead:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/exampleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="TextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- TODO: Replace with your package name -->
<com.example.PaintView
android:id="#+id/drawingBoard"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/exampleText" />
</androidx.constraintlayout.widget.ConstraintLayout>
With this approach you have to inflate the custom view in its constructors:
public class PaintView extends ConstraintLayout {
public PaintView(#NonNull Context context) {
super(context);
init();
}
public PaintView(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public PaintView(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public PaintView(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
public void init() {
inflate(getContext(), R.layout.layout_product_item, this);
}
}
To interact with the PaintView from the activity code you can do:
PaintView paintView = (PaintView) findViewById(R.id.drawingBoard);
Related
I am writing an app where I need to create a custom ImageView (in order to override its onDraw). After failing to do that for a couple of weeks I created a minimal, reproducible example which captures the problem. Here is the code:
package com.nzeldes.mytestapp;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Bundle;
import android.util.AttributeSet;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
CustomImageView myImageView;
Bitmap myBitmap;
class CustomImageView extends androidx.appcompat.widget.AppCompatImageView {
public CustomImageView(#NonNull Context context) {
super(context);
}
public CustomImageView(#NonNull Context context, #Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomImageView(#NonNull Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int displaywidth = Resources.getSystem().getDisplayMetrics().widthPixels;
myBitmap = Bitmap.createBitmap(displaywidth, displaywidth, Bitmap.Config.ARGB_8888);
myBitmap.eraseColor(Color.BLACK);
myImageView = findViewById(R.id.imageView); // Get a reference to the ImageView
myImageView.setImageBitmap(myBitmap); // Associate the bitmap to the ImageView
}
}
and the XML layout:
*<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="403dp"
android:layout_height="366dp"
android:layout_marginTop="72dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background" />
</androidx.constraintlayout.widget.ConstraintLayout>*
However, in the source window I get a “Unexpected implicit cast to CustomImageView: layout tag was ImageView” warning on myImageView = findViewById(R.id.imageView);
And if I try to run the app it crashes with this error from Logcat:
java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageView cannot be cast to com.nzeldes.mytestapp.MainActivity$CustomImageView
I admit to being new to Android (putting the lockdown times to some use learning something new!)... any help will be most appreciated.
For inner classes the syntax becomes:
<view class="{package}.{ParentClass}${innerClass}"
Please change your layout like the code below
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<view class="com.nzeldes.mytestapp.MainActivity$CustomImageView"
android:id="#+id/imageView"
android:layout_width="403dp"
android:layout_height="366dp"
android:layout_marginTop="72dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background" />
</androidx.constraintlayout.widget.ConstraintLayout>
I've got an app that works like Paint. Everything works fine but what I want to do is adding couple buttons, that would change my "brush" color and width. But since I don't have xml file, I got no idea how to do that. I know that I can do something like this : container.addView(button) but I don't have layout to grab and add button to. At least I don't know how to do that.
Here is my code:
public class Paint extends Activity {
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PaintView paintView = new PaintView(this);
setContentView(paintView);
}
}
PaintView class:
package com.example.centrummultimedialne;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
public class PaintView extends View {
public LayoutParams params;
private Path path = new Path();
private Paint brush = new Paint();
public PaintView(Context context) {
super(context);
brush.setAntiAlias(true);
brush.setColor(Color.MAGENTA);
brush.setStyle(Paint.Style.STROKE);
brush.setStrokeJoin(Paint.Join.ROUND);
brush.setStrokeWidth(8f);
params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
#Override
public boolean onTouchEvent(MotionEvent event) {
float pointX = event.getX();
float pointY = event.getY();
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
path.moveTo(pointX, pointY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(pointX, pointY);
break;
default:
return false;
}
postInvalidate();
return false;
}
#Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path, brush);
}
}
I suggest you to use a xml layout. It's a lot easier.
main.xml //Put this inside src/main/res/layout/
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.centrummultimedialne.PaintView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_weight="1.0"
android:id="#+id/paintView"/>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Color"
android:layout_weight="1.0"
android:id="#+id/btn1"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Stroke"
android:layout_weight="1.0"
android:id="#+id/btn2"/>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:layout_weight="1.0"
android:id="#+id/btn3"/>
</LinearLayout>
</LinearLayout>
Change setContentView(paintView); in Paint class to setContentView(R.layout.main);
Modify your PaintView's constructors to accept external layout.
public PaintView(Context context)
{
super(context, null);
init(context);
}
public PaintView(Context context, AttributeSet attrs)
{
super(context, attrs, 0);
init(context);
}
public PaintView(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr, 0);
init(context);
}
public PaintView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{
super(context, attrs, defStyleAttr, defStyleRes);
init(context);
}
private void init()
{
brush.setAntiAlias(true);
brush.setColor(Color.MAGENTA);
brush.setStyle(Paint.Style.STROKE);
brush.setStrokeJoin(Paint.Join.ROUND);
brush.setStrokeWidth(8f);
params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
}
Later in Paint class, set all these buttons a listener.
public class MainActivity extends Activity implements View.OnClickListener
{
PaintView paintView;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
paintView = findViewById(R.id.paintView);
((Button)findViewById(R.id.btn1)).setOnClickListener(this);
((Button)findViewById(R.id.btn2)).setOnClickListener(this);
((Button)findViewById(R.id.btn3)).setOnClickListener(this);
}
#Override
public void onClick(View btn)
{
switch(btn.getId()) {
case R.id.btn1: paintView.changeColor(Color.BLUE); break;
case R.id.btn2: paintView.setStroke(10F); break;
}
}
}
Now in PaintView class, you can implement changeColor(int Color) or any other methods you like.
My activity.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="#+id/mainWindow"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<GridLayout
android:id="#+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="2"
android:columnCount="2"
android:useDefaultMargins="true"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_column="0"
android:background="#drawable/shape"
android:gravity="center"
android:text="Hallo"
/>
</GridLayout>
</android.support.constraint.ConstraintLayout>
How I can add many of this TextFields to the GridLayout in Java.
I tried:
package com.example.sudokusolver;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.GridLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridLayout.LayoutParams lp= new GridLayout.LayoutParams();
lp.width= GridLayout.LayoutParams.MATCH_PARENT;
lp.height= GridLayout.LayoutParams.MATCH_PARENT;
lp.columnSpec= GridLayout.spec(GridLayout.UNDEFINED, 1f);
lp.rowSpec= GridLayout.spec(GridLayout.UNDEFINED, 1f);
TextView textView= new TextView(this);
textView.setBackgroundResource(R.drawable.shape);
textView.setGravity(Gravity.CENTER);
textView.setText("Hallo");
GridLayout grid= findViewById(R.id.gridLayout);
grid.addView(textView);
grid.addView(textView);
}
}
But if i add so more then 1 TextView the error come:
The specified child already has a parent. You must call removeView() on the child's parent first. How I can do this?
You can add a custom view GridChild to your GridLayout which can contain multiple TextViews like this
<?xml version="1.0" encoding="utf-8"?>
<GridLayout
android:id="#+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:rowCount="1"
android:columnCount="2"
android:useDefaultMargins="true">
<com.example.customview.GridChild/>
</GridLayout>
and your GridChild class
package com.example.customview;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
public class GridChild extends LinearLayout {
private TextView text1;
private TextView text2;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init() {
inflate(getContext(), R.layout.layout_ll, this);
this.text1 = (TextView)findViewById(R.id.textview1);
this.text2 = (TextView)findViewById(R.id.textview2);
}
}
I'm trying to figure out why an Android custom view which works when no background is set suddenly stops working when the background is set. It seems the background covers the items added to the view when it is set. I've simplified the view code to the bare minimum which reproduces the problem and to be able to post the code here. The custom view inherits from RelativeLayout and the code is as follow:
public class TestView extends RelativeLayout {
private LayoutInflater mInflater;
private ViewTreeObserver mViewTreeObserber;
private boolean mInitialized = false;
public TestView(Context ctx) {
super(ctx, null);
initialize(ctx, null, 0);
}
public TestView(Context ctx, AttributeSet attrs) {
super(ctx, attrs);
initialize(ctx, attrs, 0);
}
public TestView(Context ctx, AttributeSet attrs, int defStyle) {
super(ctx, attrs, defStyle);
initialize(ctx, attrs, defStyle);
}
private void initialize(Context ctx, AttributeSet attrs, int defStyle) {
mInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewTreeObserber = getViewTreeObserver();
mViewTreeObserber.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (!mInitialized) {
mInitialized = true;
drawItem();
}
}
});
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawItem();
}
private void drawItem() {
if (!mInitialized) return;
removeAllViews();
View item = mInflater.inflate(R.layout.testview_item, null);
TextView txt = (TextView)item.findViewById(R.id.test_view_item);
txt.setText("Test View Text");
txt.setTextColor(Color.BLACK);
txt.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
addView(item);
}
}
The item layout is simple:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:orientation="horizontal" >
<TextView
android:id="#+id/test_view_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:ellipsize="end"
android:maxLines="1" />
</LinearLayout>
And the sample app simply declares two instances of the custom view in XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView android:text="With Background set"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.machado.felipe.TestView
android:background="#android:color/holo_blue_light"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.machado.felipe.TestView>
<TextView android:text="WithOUT Background set"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<com.machado.felipe.TestView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.machado.felipe.TestView>
</LinearLayout>
The result looks like the picture bellow:
This is driving me crazy! I'm not used to write custom views in android and this is someone else's code which I'm trying to fix! I don't even know if this is the way it should be done, since I'm inflating views and adding them to the RelativeLayout I don't think I should be adding them in the onDraw, but since the complete code is doing more complex stuff, as laying out the items in multiple rows with wrapping, it is possibly a valid approach... But, anyway, I can't figure out how to fix this!
I'm working on a simple project for my studies and I’m stuck with this problem.
I'm building Snakes And Ladders app and I’m trying to make my player (PNG image) to move around the board an animate.
I want the animation to happen over the game’s board background which I defined in an xml file and I just can’t do it.
The program that I will attach is not working, I have no idea why. In addition I need to add the part that takes the xml background in consideration, that part is missing and I will greatly appreciate if someone can help me solve this problem.
Thanks in advanced.
The board xml file(game.xml):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/easymap"
android:orientation="vertical" >
<TextView
android:id="#+id/whitePlayer"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#drawable/white"
android:visibility="gone" />
<TextView
android:id="#+id/blackPlayer"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#drawable/black"
android:visibility="gone" />
<Button
android:id="#+id/btRoll"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/cubePic"
android:background="#drawable/buttonshape"
android:text="Roll"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/cubePic"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="42dp"
android:layout_marginRight="22dp"
android:background="#drawable/cube" />
<TextView
android:id="#+id/tvTurn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cubePic"
android:layout_alignRight="#+id/btRoll"
android:text="Your turn!"
android:textColor="#color/green"
android:textSize="32dp"
android:textStyle="italic" />
</RelativeLayout>
The java class:
package com.example.snakesnladders;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class GFX_Game extends Activity implements OnClickListener {
TextView whitePlayer, blackPlayer;
Button roll;
TextView cube, map1, map2, map3, label;
boolean yourTurn = true;
MyBringBack ourView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ourView = new MyBringBack(this);
setContentView(ourView);
init();
}
private void init() {
cube = (TextView) findViewById(R.id.cubePic);
roll = (Button) findViewById(R.id.btRoll);
label = (TextView) findViewById(R.id.tvTurn);
roll.setOnClickListener(this);
}
#Override
protected void onDestroy() {
super.onDestroy();
}
#Override
public void onClick(View view) {
int rand = (int) (Math.random() * 6) + 1;
}
class MyBringBack extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
Bitmap backGround, playerB, playerW;
boolean isRunning = true;
public MyBringBack(Context context) {
super(context);
playerW = BitmapFactory.decodeResource(getResources(),
R.id.whitePlayer);
ourHolder = getHolder();
ourThread = new Thread(this);
ourThread.start();
}
#Override
public void run() {
while (isRunning) {
if (!ourHolder.getSurface().isValid())
continue;
Canvas canvas = ourHolder.lockCanvas();
canvas.drawBitmap(playerW, 0, 0, null);
ourHolder.unlockCanvasAndPost(canvas);
}
}
}
}
The changed inner class:( I've extended View an override the onDraw method from the Viev class, and it still not working)
In addition I wanted to draw on my existing xml layout that i created in the game.xml file.
class MyBringBack extends View {
Bitmap playerW;
public MyBringBack(Context context) {
super(context);
playerW = BitmapFactory.decodeResource(getResources(),
R.id.whitePlayer);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(playerW,0,0,null);
}
}
Create a custom View as follows. If you're root view is RelativeLayout and you need to draw on it, extend a RelativeLayout
public final class MyRelativeLayout extends RelativeLayout {
Bitmap playerW;
public MyRelativeLayout(Context context) {
super(context);
init(context);
}
public MyRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MyRelativeLayout(Context context, AttributeSet attrs, int style) {
super(context, atts, style);
init(context);
}
private void init(final Context context) {
playerW = BitmapFactory.decodeResource(context.getResources(),
R.id.whitePlayer);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(playerW,0,0,null);
}
}
Then use it in xml as follows
<?xml version="1.0" encoding="utf-8"?>
<your.package.name.package.MyRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
all the stuff here
<your.package.name.package.MyRelativeLayout>