Android custom view not showing - java

I am working on an Android version of Gomoku. I am quite new to Java in general and even more so to Android. I am following a book called "Hello Android" where the author teaches the basics through making a game of Sudoku. I am following it loosely, leaving out features not needed for my Gomoku. However, a new view is made when New Game is pressed and although the book continues on as if it should be working, what should be drawn is not showing for me at all. Here is the code that deals with the stuff:
Mainactivity.java:
private void startGame() {
Log.d(TAG, "Clicked New Game");
Intent intent = new Intent(this, Game.class);
startActivity(intent);
}
Game.java:
public class Game extends Activity {
private static final String TAG = "Game";
private int board[] = new int[10 * 10];
private GameView gameView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Game.onCreate called");
gameView = new GameView(this);
gameView.requestFocus();
Log.d(TAG, "Game.onCreate finished");
}
}
GameView.java:
public class GameView extends View {
private static final String TAG = "Game";
private float width; //Width of one tile
private float height; //Height of one tile
private final Game game;
Paint background = new Paint();
Paint dark = new Paint();
Paint light = new Paint();
Paint hilite = new Paint();
public GameView(Context context) {
super(context);
this.game = (Game) context;
setFocusable(true);
setFocusableInTouchMode(true);
Log.d(TAG, "GameView finished");
}
#Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w / 10f;
height = h /10f;
Log.d(TAG, "onSizeChanged: width " + width + ", height " + height);
}
#Override
protected void onDraw(Canvas canvas) {
//Draw the background
background.setColor(getResources().getColor(R.color.background));
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
//Draw the board
//Define colors for grid lines
dark.setColor(getResources().getColor(Color.DKGRAY));
light.setColor(getResources().getColor(Color.LTGRAY));
hilite.setColor(getResources().getColor(Color.WHITE));
for (int i = 0; i < 10; i++) {
Log.d(TAG, "Drawing...");
canvas.drawLine(0, i * height - 1, getWidth(), i * height - 1, light);
canvas.drawLine(0, i * width - 1, getHeight(), i * width - 1, light);
canvas.drawLine(0, i * height, getWidth(), i * height, hilite);
canvas.drawLine(0, i * width, getHeight(), i * width, hilite);
canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1, dark);
canvas.drawLine(0, i * width + 1, getHeight(), i * width + 1, dark);
}
}
}
I have tried comparing the author's code to mine and except where I am not implementing features he is the codes seem to be matching.
However, the Log.d(TAG, "onSizeChanged: width " + width + ", height " + height); does not appear in LogCat so I assume this function is simply never called and I do not understand why.

You just create an instance of your GameView but you do not add it to your activity. Do this by using
setContentView(gameView);
in your onCreate method.

You need to set the view to the activity with setContentView(gameView) in onCreate of Game Activity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(TAG, "Game.onCreate called");
gameView = new GameView(this);
setContentView(gameView); // missing
...//rest of the code

Related

Android PathShape not drawing path (Canvas/Java/Android Studio)

I'm just trying to draw a basic triangle in Android Studio but nothing is showing up when I run the program. This is my first Android program so I don't really know what I'm doing. What is wrong with it?
public class CustomDrawableView extends View {
private ShapeDrawable mDrawable;
public CustomDrawableView(Context context, Display display) {
super(context);
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;
int pathWidth = screenWidth / 6;
int startWidth = screenWidth / 10;
Path path = new Path();
path.moveTo(startWidth, 0);
path.moveTo(startWidth, screenHeight / 5);
path.moveTo(screenWidth / 5, screenHeight / 5);
path.close();
mDrawable = new ShapeDrawable(new PathShape(path, screenWidth, screenHeight));
mDrawable.setDither(true);
int color = Color.parseColor("#ff74AC23");
mDrawable.getPaint().setColor(color);
mDrawable.setBounds(0, 0 , screenWidth, screenHeight);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
}
public class MainActivity extends AppCompatActivity {
CustomDrawableView mCustomDrawableView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = getWindowManager().getDefaultDisplay();
mCustomDrawableView = new CustomDrawableView(this, display);
setContentView(mCustomDrawableView);
}
}

Why is the SurfaceView cut if I resume app?

If I use an Android L device and resume my app, the SurfaceViews are cut.
The details of occurrence condition are as follows:
- Using Android L (It is originally L, not updated to L)
- The SurfaceView is larger than the display.
- The left-top of SurfaceView is more to the left or top of the display.
- Resuming the app from the background apps list, not the icon in the app list.
I tried to reset size, gravity, clip bounds, etc., however, none had effect.
I want to repair the SurfaceView cut, you do not have some idea?
The sample code is as follows:
[Activity]
public class ViewTestActivity extends Activity {
private DrawSurfaceView mDrawSurfaceView;
private int overDispW;
private int overDispH;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
Point dispP = new Point();
display.getSize(dispP);
overDispW = (int) (dispP.x * 1.5);
overDispH = (int) (dispP.y * 1.5);
FrameLayout.LayoutParams overSizeParams = new FrameLayout.LayoutParams(overDispW, overDispH);
//If gravity is Gravity.LEFT|Gravity.TOP, there is no cut.
overSizeParams.gravity = Gravity.CENTER;
mDrawSurfaceView = new DrawSurfaceView(this);
setContentView(mDrawSurfaceView, overSizeParams);
}
}
[SurfaceView]
public class DrawSurfaceView extends SurfaceView implements SurfaceHolder.Callback{
private Paint mPaintRed;
private Paint mPaintBlue;
private Paint mPaintGreen;
private Paint mPaintYellow;
private SurfaceHolder mHolder;
public DrawSurfaceView(Context context) {
super(context);
mPaintRed = new Paint();
mPaintRed.setColor(Color.argb(255, 255, 0, 0));
mPaintBlue = new Paint();
mPaintBlue.setColor(Color.argb(255, 0, 0, 255));
mPaintGreen = new Paint();
mPaintGreen.setColor(Color.argb(255, 0, 255, 0));
mPaintYellow = new Paint();
mPaintYellow.setColor(Color.argb(255, 255, 255, 0));
mHolder = getHolder();
mHolder.setFormat(PixelFormat.RGBA_8888);
mHolder.addCallback(this);
}
public void onDraw(Canvas canvas){
super.onDraw(canvas);
drawRect(canvas);
}
private void drawRect(Canvas canvas) {
int w = canvas.getWidth();
int h = canvas.getHeight();
canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
canvas.drawRect(0, 0, w/2, h/2, mPaintRed);
canvas.drawRect(w/2, h/2, w, h, mPaintBlue);
canvas.drawRect(w/2, 0, w, h/2, mPaintGreen);
canvas.drawRect(0, h/2, w/2, h, mPaintYellow);
}
public void doDraw(){
Canvas canvas = mHolder.lockCanvas();
if(canvas != null){
drawRect(canvas);
mHolder.unlockCanvasAndPost(canvas);
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
doDraw();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
doDraw();
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
Right side of view has been cut.

How to design a simple game map in Android?

I'm making a very very simple game in Android, and I'm not using OpenGL or any other libraries, just Java!
Now I'm stuck in designing my map...
I want to design something like this:
(Those lines should consider as walls and players can't get out of them)
How to design something like this using XML only? Or I should start using Unity3D?
This isn't done with xml, but you neither need any libraries.
public class Background extends View {
private static final int BORDER_WIDTH = 2; //in px
private int[] points; //2n = x, 2n+1 = y
public Background(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(Drawable drawable : drawBackground()){
drawable.draw(canvas);
}
}
private Drawable[] drawBackground() {
Drawable[] drawables = new Drawable[2];
int width = getWidth();
int height = getHeight();
int corner = width / 4;
//draw border
Path path = new Path();
path.moveTo(0, corner);
path.lineTo(corner, 0);
path.lineTo(width, 0);
path.lineTo(width, height - corner);
path.lineTo(width - corner, height);
path.lineTo(0, height);
path.close();
drawables[0] = new ShapeDrawable(new PathShape(path, width, height));
drawables[0].setBounds(0, 0, width, height);
//draw inside the border
points = {
BORDER_WIDTH, corner,
corner, BORDER_WIDTH,
width - BORDER_WIDTH, BORDER_WIDTH,
width - BORDER_WIDTH, height - corner,
width - corner, height - BORDER_WIDTH,
BORDER_WIDTH, height - BORDER_WIDTH
}
path = new Path();
path.moveTo(points[0], points[1]);
for (int i = 2, i < points.length, i++) {
path.lineTo(points[i], points[++i]);
}
path.close();
ShapeDrawable shapeDrawable = new ShapeDrawable(new PathShape(path, width, height));
shapeDrawable.getPaint().setColor(Color.rgb(238, 238, 238));
shapeDrawable.setBounds(0, 0, width, height);
drawables[1] = shapeDrawable;
return drawables;
}
public Point[] getPoints() {
return points;
}
}
The result:

How can I center this rectangle and image in the y axis?

So far, my image starts at 0,0 and scales properly. There is a black bar under it. How can I align it so that it is centered along the y, with a black bar above and below the image?
edit: also, what are "this.getWidth(), this.getHeight()" referring to when I make the object "dest" - the width of mBitmap? The documentation does say they represent the width of the view, I just want to make sure I'm correct in thinking that is it mBitmap.
public class SpotGameActivity extends Activity {
private Bitmap mBitmap;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dpi2b);
setContentView(new BitMapView(this, mBitmap));
}
class BitMapView extends View {
Bitmap mBitmap = null;
public BitMapView(Context context, Bitmap bm) {
super(context);
mBitmap = bm;
}
#Override
protected void onDraw(Canvas canvas) {
//DisplayMetrics metrics = new DisplayMetrics();
//getWindowManager().getDefaultDisplay().getMetrics(metrics);
// called when view is drawn
Paint paint = new Paint();
paint.setFilterBitmap(true);
// The image will be scaled so it will fill the width, and the
// height will preserve the image’s aspect ration
float aspectRatio = ((float) mBitmap.getWidth()) / mBitmap.getHeight();
Rect dest = new Rect(0, 0, this.getWidth(),(int) (this.getWidth() / aspectRatio));
String AR = Double.toString(aspectRatio);
canvas.drawBitmap(mBitmap, null, dest, paint);
Toast.makeText(SpotGameActivity.this, AR, Toast.LENGTH_SHORT).show();
}
I've now gotten the difference between the view size and image size. I've divided the difference by 2 but now, my image is align to the bottom of the screen rather than the middle of the y-axis. any tips?:
#Override
protected void onDraw(Canvas canvas) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int scrHeight = this.getHeight();
String sH = Integer.toString(scrHeight);
Log.v(TAG, "scrHeight =" + sH );
int imgHeight= mBitmap.getHeight();
String iH = Integer.toString(imgHeight);
Log.v(TAG, "imgHeight =" + iH );
int diff = scrHeight - imgHeight;
String dif = Integer.toString(diff);
Log.v(TAG, "diff =" + dif );
int dvd= diff/2;
// called when view is drawn
Paint paint = new Paint();
paint.setFilterBitmap(true);
// The image will be scaled so it will fill the width, and the
// height will preserve the image’s aspect ration
float aspectRatio = ((float) mBitmap.getWidth()) / mBitmap.getHeight();
Rect dest = new Rect(0, dvd, this.getWidth(),(int) (this.getWidth() / aspectRatio)+dvd);
String AR = Double.toString(dvd);
canvas.drawBitmap(mBitmap, null, dest, paint);
Toast.makeText(SpotGameActivity.this, AR, Toast.LENGTH_SHORT).show();

Aspect ratio is off when scaling down and drawing a bitmap

The image I am using is from a 4x6. So the aspect ratio should be about .66. When I calculate the aspect ration in my code , I get around .66. However, the height of the displayed image looks squashed. The image only looks right if I set the aspect ratio manually to around .85.
Since setting the aspect ratio manually is not perfect, how can I get the can I keep the height of my image from appearing to be squashed.
public class SpotGameActivity extends Activity {
private Bitmap mBitmap;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dpi2b);
setContentView(new BitMapView(this, mBitmap));
}
class BitMapView extends View {
Bitmap mBitmap = null;
public BitMapView(Context context, Bitmap bm) {
super(context);
mBitmap = bm;
}
#Override
protected void onDraw(Canvas canvas) {
// called when view is drawn
Paint paint = new Paint();
paint.setFilterBitmap(true);
// The image will be scaled so it will fill the width, and the
// height will preserve the image’s aspect ration
float aspectRatio = ((float) mBitmap.getWidth()) / mBitmap.getHeight();
Rect dest = new Rect(0, 0, this.getWidth(),(int) (this.getHeight() * aspectRatio));
String AR = Double.toString(aspectRatio);
//Rect dest = new Rect(0, 0, getWidth(), getHeight());
canvas.drawBitmap(mBitmap, null, dest, paint);
Toast.makeText(SpotGameActivity.this, AR, Toast.LENGTH_SHORT).show();
}
}
}
Aspect = width/height
newHeight = newWidth/Aspect
Do this,
Rect dest = new Rect(0, 0, this.getWidth(),(int) (this.getWidth() / aspectRatio));

Categories

Resources