How to turn off Anti-Aliasing of an Imageview in Android Java - 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);

Related

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.

NEED some guides on moving actors on stage libgdx

i am currently facing a problem where i try to move my actor by touchdown and i am confuse and did some error. I am trying to move my bucket to the left and right by dragging the actor.
This is my code
package com.tcyzzz.savethesemester;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;
import com.badlogic.gdx.utils.viewport.ScreenViewport;
public class MyGdxGame extends ApplicationAdapter {
Stage stage;
#Override
public void create () {
stage = new Stage(new ScreenViewport());//establish a stage for the game
MyActor actor = new MyActor();
stage.addActor(actor);
stage.setKeyboardFocus(actor);
actor.addListener(new InputListener(){
#Override
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
MoveByAction mba = new MoveByAction();
mba.setAmount(5,0);
stage.addAction(mba);
return true;
}
});
Gdx.input.setInputProcessor(stage);//handle user input
}
#Override
public void render () {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.draw();// call all sprites in actors class
}
#Override
public void dispose () {
stage.dispose();//stage dispose
}
}
and my actors class
package com.tcyzzz.savethesemester;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.actions.MoveByAction;
class MyActor extends Actor {
Sprite sprite = new Sprite(new Texture(Gdx.files.internal("bucket.png")));
public MyActor(){
setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
setTouchable(Touchable.enabled);
}
#Override
public void draw(Batch batch, float parentAlpha) {
sprite.draw(batch);
setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
}
#Override
public void act(float delta) {
super.act(delta);
}
}
i need a clear instruction on how to move the bucket by dragging it using stage , actors .. thanks
#Tenfour04 is right
instead
setBounds(sprite.getX(),sprite.getY(),sprite.getWidth(),sprite.getHeight());
should use:
setBounds(getX(),getY(),sprite.getWidth(),sprite.getHeight());

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.

timer, refresher

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.

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

Categories

Resources