Reputation: 1
I'm pretty noob in Java and Android Studio, and i just can't find a good way to draw a rectangle. Why has Java got to be so hard?! I have a button in my main_activity that opens a new activity. This one:
package com.example.marty.pongtest;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
public class GamePanelSingle extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_game_panel_single);
}
public class drawRectangle extends View{
public drawRectangle(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Rect paddle = new Rect();
paddle.set(0, 0, canvas.getWidth()/2, canvas.getHeight()/2);
Paint white = new Paint();
white.setColor(Color.WHITE);
white.setStyle(Paint.Style.FILL);
canvas.drawRect(paddle,white);
}
}
My Xml code:
<?xml version="1.0" encoding="utf-8"?>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="GameSinglePlayer"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="55dp"
android:layout_marginStart="55dp"
android:layout_marginTop="22dp"
android:textColor="#FFFFFF" />
why doesn't this work, and how do I get it to work?
Thx..
Upvotes: 0
Views: 2701
Reputation: 12073
How about using a TextView like this wherever needed:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="horizontal">
<TextView
android:layout_width="10dp"
android:layout_height="20dp"
android:background="@color/light_green"/>
</LinearLayout>
Upvotes: 0
Reputation: 982
It is not that easy to understand your code. Its not well programmed. At first pls write class names first letter in capital. Secondly dont call a class sth like DrawRectangle. Its not a method. A class is a pattern for instances you generate. A better name would be Drawer/Brush or sth like that. Because everyone who looks at your code and reads drawRectangle thinks that this must be a method. However: Once I implemented a small Activity with a net of rectangles that are connected by lines. Here is the simple structure how i did that. I reduced the code as most as possible. This easily draws some wild lines:
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import java.util.ArrayList;
import java.util.List;
public class ZoomView extends View{
private float mPosX = 0;
private float mPosY = 0;
Context context;
Paint red;
public ZoomView(Context context) {
super(context);
this.context = context;
init();
}
public ZoomView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
init();
}
public ZoomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
init();
}
public void init(){
red = new Paint();
red.setColor(getResources().getColor(R.color.accent));
red.setAntiAlias(true);
red.setStrokeWidth(3);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.translate(mPosX, mPosY);
drawLines(canvas);
canvas.restore();
}
public void drawLines(Canvas canvas){
canvas.drawLine(50, 0, 50, -175, red);
canvas.drawLine(50, 25, 50, 200, red);
canvas.drawLine(50, 250, 200, 350, red);
canvas.drawLine(50, 250, -100, 350, red);
canvas.drawLine(-150, 375, -300, 375, red);
canvas.drawLine(-100, 400, -100, 550, red);
canvas.drawLine(0, -175, -100, -50, red);
canvas.drawLine(100, -175, 200, -50, red);
canvas.drawLine(-100, -300, 0, -175, red);
canvas.drawLine(100, -175, 200, -300, red);
canvas.drawLine(50, -200, 50, -375, red);
canvas.drawLine(-100, 600, -100, 750, red);
canvas.drawLine(-100, 600, -250, 700, red);
canvas.drawLine(-100, 600, 50, 700, red);
canvas.drawLine(200, 400, 200, 575, red);
canvas.drawLine(225, 375, 425, 375, red);
canvas.drawLine(-150, -25, -300, -25, red);
canvas.drawLine(-100, 0, -250, 100, red);
canvas.drawLine(-350, 0, -500, 100, red);
canvas.drawLine(-350, -25, -550, -25, red);
canvas.drawLine(-350, -50, -500, -150, red);
canvas.drawLine(250, -25, 400, -25, red);
canvas.drawLine(200, -50, 350, -150, red);
canvas.drawLine(200, 0, 350, 100, red);
canvas.drawLine(250, -325, 400, -325, red);
canvas.drawLine(200, -350, 350, -450, red);
canvas.drawLine(500, -325, 650, -325, red);
canvas.drawLine(450, -300, 600, -200, red);
canvas.drawLine(50, -400, 125, -600, red);
canvas.drawLine(50, -400, -25, -600, red);
canvas.drawLine(50, -400, -100, -500, red);
canvas.drawLine(50, -400, 200, -500, red);
canvas.drawLine(-100, -300, -250, -200, red);
canvas.drawLine(-150, -325, -300, -325, red);
canvas.drawLine(-100, -350, -250, -450, red);
}
}
XML:
<LinearLayout 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">
<XXX.XXX.XXX.ZoomView
android:id="@+id/zoomView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Upvotes: 0
Reputation: 982
Easy peacy:
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
//Draw something
canvas.restore();
}
Upvotes: 1
Reputation: 725
Please paste your layout xml. And if you want to simply show a rectangle, you can just define a rectangle shape in drawable folder, and refer it in your layout, no java code needed.
Upvotes: 0