0
\$\begingroup\$

I want to implement glide in my game but I don't know how. I know that the Glide format is:

Glide.with(context).load(R.drawable.image).into(imageView);

But I don't understand how to implement it in my game.class

Game.class:

public class Game extends SurfaceView { Player player; Paint paint; Bitmap image_player; public Game(Context context, AttributeSet attrs) { super(context, attrs); paint = new Paint(); image_player = BitmapFactory.decodeResource(getResources(), R.drawable.player); player = new Player(this, image_player); } public void onDraw(Canvas canvas) { paint.setColor(Color.BLACK); canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint); player.onDraw(canvas); invalidate(); } } 

Player.Class:

public class Player { private Game game; private Bitmap player; Paint paint; int x, y, start = 0, width, height, currentFrame = 0,Horizontal = 1, Vertical = 8, frameLengthInMilliseconds = 150, cantFrames = 8; long fps, timeThisFrame, lastFrameChangeTime = 0; public Player(Game juego, Bitmap player){ this.game = game; this.player = player; this.width = player.getWidth() / Vertical; this.height = player.getHeight() / Horizontal; paint = new Paint(); } private void update() { long startFrame = System.currentTimeMillis(); if (startFrame > lastFrameChangeTime + frameLengthInMilliseconds) { lastFrameChangeTime = startFrame; currentFrame++; if (currentFrame >= cantFrames) { currentFrame = 0; } } long startFrameTime = System.currentTimeMillis(); timeThisFrame = System.currentTimeMillis() - startFrameTime; if (timeThisFrame >= 1) { fps = 1000 / timeThisFrame; } } public void onDraw(Canvas canvas) { update(); int srcX = currentFrame * width; int srcY = height * start; x = canvas.getWidth()/2; y = canvas.getHeight()/2; Rect src = new Rect(srcX, srcY, srcX + width, srcY + height); Rect dst = new Rect(x, y, x + width, y + height); canvas.drawBitmap(player, src, dst, null); } } 

XML:

<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.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"> <com.example.agusv.MyProject.Game android:layout_width="match_parent" android:layout_height="match_parent" android:background="@android:color/transparent"/> </android.support.constraint.ConstraintLayout> 
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

glide is used to set the image on an ImageView

you need to define an image view in your layout

<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/imageView"> </ImageView> 

and then you can set an image on this image view using glide:

ImageView imageView = (ImageView) findViewById(R.id.imageView); Glide.with(this).load(R.drawable.image).into(imageView); 

there's a more detailed explanation on this page

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.