timer, refresher - java

how do i develop an app that draws a line on screen at fixed coordinates, Setting up a repeating timer of 1 second duration? On every tick of the timer,the line refreshes.
LineRefresh.java:
package LineRefresh.xyz.com;
import java.util.Timer;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
public class LineRefresh extends Activity {
DrawView drawView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawView = new DrawView(this);
drawView.setBackgroundColor(Color.WHITE);
setContentView(drawView);
}
}
DrawView.java:
package LineRefresh.xyz.com;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
super(context);
}
#Override
public void onDraw(Canvas canvas) {
paint.setColor(Color.BLACK);
canvas.drawLine(50, 200, 270, 200, paint);
}
}

Use a Handler and use it's postDelayed() to schedule drawing a line.
After you draw a line also schedule another postDelayed() to continue this process.

Related

How to turn off Anti-Aliasing of an Imageview in Android Java

I searched the net a lot of time, to make a code like this:
import android.app.Activity;
import android.os.Bundle;
import android.graphics.DrawFilter;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
import android.graphics.drawable.DrawableWrapper;
import android.graphics.drawable.Drawable;
import android.graphics.*;
public class math_math extends DrawableWrapper {
private final DrawFilter DRAW_FILTER =
new PaintFlagsDrawFilter(Paint.FILTER_BITMAP_FLAG, 0);
public math_math(Drawable wrapped) {
super(wrapped);
wrapped.setFilterBitmap(false);
}
#Override
public void draw(Canvas canvas) {
super.draw(canvas);
}
}
In the MainActivity onCreate i run this code to turn off anti aliasing:
imageview1.setImageDrawable(new math_math(getDrawable(R.drawable.auto)));
By the way, the auto is the picture, that i want to see with turned off anti aliasing
BUT there is a big problem
The app did this with the image:
https://i.stack.imgur.com/8IlB6.png
But the image should look like this:
https://i.stack.imgur.com/UnUzg.png
Please somebody help, what's the problem in my code.
Thanks
You have to do MANUAL scaling if you want to display a small image in a bigger View without trigger the Bitmap auto-scaling:
final Bitmap cSmallBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.auto);
final Bitmap cBigBitmap = Bitmap.createBitmap(600, 600, Bitmap.Config.ARGB_8888);
final Canvas cCanvas = new Canvas(cBigBitmap);
final Paint cPaint = new Paint();
cPaint.setAntiAlias(false);
cPaint.setDither(false);
cPaint.setFilterBitmap(false);
cCanvas.drawBitmap(cSmallBitmap, new Rect(0, 0, cSmallBitmap.getWidth(), cSmallBitmap.getHeight()), new Rect(0, 0, cBigBitmap.getWidth(), cBigBitmap.getHeight()), cPaint);
cImageView.setImageBitmap(cBigBitmap);

Why won't this picture load in, in LibGDX, I can't find anything to help fix this on youtube and other websites

I am trying to make a picture load in, using LibGdx, but I keep getting this error when I try.
I don
t understand what this error means, and I can't find the answer to it on youtube.
error: cannot find symbol
private Texture img;
^
symbol: class Texture
location: class AndroidLauncher
package com.mygdx.game;
import android.os.Bundle;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.mygdx.game.MyGdxGame;
public class AndroidLauncher extends AndroidApplication {
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
initialize(new MyGdxGame(), config);
}
private Texture img;
private Sprite sprite;
public void create () {
batch = new SpriteBatch();
img = new Texture("sadgameslogo1.png");
sprite = new Sprite(img);
}
public void render () {
Gdx.gl.glClearColor(1,1,1,1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
sprite.draw(batch);
batch.end();
}
}
Try adding import com.badlogic.gdx.graphics.Texture; to the imports.

must implement the inherited method ApplicationListener.render()

I keep getting this error:
"The type MainMenu must implement the inherited abstract method ApplicationListener.render()"
MY code:
package com.gdx.game.Screens;
import java.util.ArrayList;
import aurelienribon.tweenengine.BaseTween;
import aurelienribon.tweenengine.Tween;
import aurelienribon.tweenengine.TweenCallback;
import aurelienribon.tweenengine.TweenEquations;
import aurelienribon.tweenengine.TweenManager;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.gdx.game.Asteroid;
import com.gdx.game.TweenAccessors.SpriteTween;
public class MainMenu implements ApplicationListener, Screen
{
//private Boolean isMain = false; //Boolean to check if the menu screen can exit
private Texture menuTexture; //Splash screen image
private Sprite menuSprite; //Image manipulation
private SpriteBatch batch; //Container to bundle Sprites to speed up performance
private Asteroid game;
private TweenManager manager; //Updates all tweens at once
private TweenCallback callBack;
//SplashScreen constructor
public MainMenu(Asteroid game)
{
this.game = game;
}
#Override
public void render(float delta)
{
Gdx.gl.glClearColor(0,0,0,1); //Set color to black
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //Clear the color
manager.update(delta);
//Begin drawing
batch.begin();
if((Gdx.input.isKeyPressed(Keys.ENTER)))
{
batch.dispose();
//game.setScreen(gameClass(game));
}
menuSprite.draw(batch);
//End drawing
batch.end();
}
I can still run my code but when I try and press enter It terminates and I get the error:
Exception in thread "LWJGL Application" java.lang.IllegalArgumentException: buffer not allocated with newUnsafeByteBuffer or already disposed
at com.badlogic.gdx.utils.BufferUtils.disposeUnsafeByteBuffer(BufferUtils.java:277)
at com.badlogic.gdx.graphics.glutils.VertexArray.dispose(VertexArray.java:71)
at com.badlogic.gdx.graphics.Mesh.dispose(Mesh.java:415)
at com.badlogic.gdx.graphics.g2d.SpriteBatch.dispose(SpriteBatch.java:1094)
at com.gdx.game.Screens.MainMenu.render(MainMenu.java:50)
at com.badlogic.gdx.Game.render(Game.java:46)
at com.gdx.game.Asteroid.render(Asteroid.java:24)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:202)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:131)
AL lib: ReleaseALC: 1 device not closed
any ideas?
This is not the render method defined in ApplicationListener:
#Override
public void render(float delta)
The real render method has no arguments.
There should be a warning in your editor/compiler that the #Override annotation triggered.

How can I use a custom bitmap for the "you are here" point in a MyLocationOverlay?

I've poured over the docs and haven't been able to figure this out. Is it even possible?
Please see this
It looks like the correct mechanism to do this is to extend MyLocationOverlay then override the drawMyLocation() protected method.
The following uses an arrow to show where "you" are and which way "you" are pointing:
package com.example;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Point;
import android.location.Location;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
public class MyCustomLocationOverlay extends MyLocationOverlay {
private Context mContext;
private float mOrientation;
public MyCustomLocationOverlay(Context context, MapView mapView) {
super(context, mapView);
mContext = context;
}
#Override
protected void drawMyLocation(Canvas canvas, MapView mapView, Location lastFix, GeoPoint myLocation, long when) {
// translate the GeoPoint to screen pixels
Point screenPts = mapView.getProjection().toPixels(myLocation, null);
// create a rotated copy of the marker
Bitmap arrowBitmap = BitmapFactory.decodeResource( mContext.getResources(), R.drawable.arrow_green);
Matrix matrix = new Matrix();
matrix.postRotate(mOrientation);
Bitmap rotatedBmp = Bitmap.createBitmap(
arrowBitmap,
0, 0,
arrowBitmap.getWidth(),
arrowBitmap.getHeight(),
matrix,
true
);
// add the rotated marker to the canvas
canvas.drawBitmap(
rotatedBmp,
screenPts.x - (rotatedBmp.getWidth() / 2),
screenPts.y - (rotatedBmp.getHeight() / 2),
null
);
}
public void setOrientation(float newOrientation) {
mOrientation = newOrientation;
}
}
I made a few changes to the previuos code in order to get it to work properly because the arrow was pointing to the wrong direction and rotating in the opposite direction.
I changed
matrix.postRotate(mOrientation);
for
matrix.postRotate(this.getRotation());
and added to the end:
mapView.postInvalidate();
to redraw the arrow when it changes

How to create a CanvasView object

I got a class CanvasView :
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import java.util.Random;
public class CanvasView extends View {
private Canvas canvas;
private Paint paint;
private int width;
private int height;
Random random = new Random();
public CanvasView(Context context,AttributeSet attrs) {
super(context);
super.onDraw(canvas);
initPaint(); //here i just create my Paint
}
#Override
public void onDraw(Canvas canvas) {
this.canvas = canvas;
}
public void drawRandom() {
canvas.drawCircle(random.nextInt(700), random.nextInt(700), random.nextInt(200), paint);
}
}
Then i try to create CanvasView object in another activity like this:
canvasView = new CanvasView(this,null);
and then i try to call method drawRandom()
canvasView.drawRandom();
then happens error:Attempt to invoke virtual method 'void android.graphics.Canvas.drawCircle(float, float, float, android.graphics.Paint)' on a null object reference
i know that it means that canvas=null at this moment but how to fix it?I tried a lot but still cant do it.
Dont blame me if this question is as stupid,as me, i am beginner,sorry.
public CanvasView(Context context, AttributeSet attrs) {
super(context);
initPaint(); //here i just create my Paint
}
#Override
public void onDraw(Canvas canvas) {
this.canvas = canvas;
canvas.drawCircle(random.nextInt(700), random.nextInt(700), random.nextInt(200), paint);
}
public void drawRandom() {
invalidate();
}
BTW, don't forget to add it to window after New CanvasView().
Like this:
ViewGroup viewGroup =(ViewGroup) ((ViewGroup)findViewById(android.R.id.content)).getChildAt(0);
viewGroup.addView(canvasView);

Categories

Resources