I've been trying to draw a Canvas on an ImageView but after creating a bitmap I see no Image on my screen. The only thing I get is the small circle I've drawn in the onDraw Method. I want to have the Canvas on the ImageView.
I've got the following code:
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Bitmap myBitmap;
Paint paint;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
}
public void onClickButton(View view) {
View v = new DrawCanvas(getApplicationContext());
Bitmap bitmap = Bitmap.createBitmap(48, 48, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v.draw(canvas);
imageView = (ImageView) findViewById(R.id.testpicture);
imageView.setImageBitmap(bitmap);
}
}
and
public class DrawCanvas extends View {
Paint paint = new Paint();
public DrawCanvas(Context context) {
super(context);
}
public DrawCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.RED);
canvas.drawCircle(25, 25, 5, paint);
}
}
Thanks in advance!
First, you're overcomplicating things. You don't need, or want, a view to do what you're doing. If you want to draw a circle, do it directly to the canvas by using canvas.drawCircle in your onClick.
Second, alling setImageBitmap overwrites any previous bitmap you set. If you want this to show over the previous image, you have 2 options:
1)Use two image views on top of one another, and set the top image to the bitmap made on the canvas (to make this work you may need to make sure the background color on the bitmap is transparent).
2)Create a new view subclassed off ImageView that displays 2 bitmaps by overriding onDraw.
The first approach is probably easier.
Related
I have a CardView (with multiple items like imageView, textView, Buttons) in a RecyclerView. I want to get bitmap from this cardview. I tried a simple method of converting cardView into bitmap (convert a cardview to bitmap) but its not working properly. I only got the raw view of the card just like in xml without any updated items from a firebaseserver.
CardView extend view, so you probably can convert it as any other view,
try to pass the CardView into that function:
public static Bitmap getBitmapFromView(View view) {
//Define a bitmap with the same size as the view
Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
//Bind a canvas to it
Canvas canvas = new Canvas(returnedBitmap);
//Get the view's background
Drawable bgDrawable =view.getBackground();
if (bgDrawable!=null)
//has background drawable, then draw it on the canvas
bgDrawable.draw(canvas);
else
//does not have background drawable, then draw white background on the canvas
canvas.drawColor(Color.WHITE);
// draw the view on the canvas
view.draw(canvas);
//return the bitmap
return returnedBitmap;
}
you are probably taking the screenshot of view before it is fully inflated. Try adding a 2 second delay or implement this on layout listener of the card view
In my program, I'm creating shapes which are filled with random colors from a list. The colors are set in onCreate. I don't want it to have determined colors, but to change the colors multiple times. How do I 'restart' the onCreate part, so the colors are allocated again?
public class MainActivity extends Activity implements OnGestureListener
{
private Paint paint = new Paint();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Canvas canvas = new Canvas(bg);
List<Integer> numbers = Arrays.asList(Color.RED, Color.BLUE, Color.GREEN, Color.YELLOW);
Collections.shuffle(numbers);
}
}
Here is the trick, call this method
loadColors();
in oncreate and anywhere else u want
public class MainActivity extends Activity implements OnGestureListener
{
private Paint paint = new Paint();
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Canvas canvas = new Canvas(bg);
loadColors();
}
private void loadColors(){
List<Integer> numbers = Arrays.asList(Color.RED, Color.BLUE,
Color.GREEN, Color.YELLOW);
Collections.shuffle(numbers);
}
}
this may help you, just put in your onResume or the method you want to restart oncreate from
onCreate(new Bundle());
for example by clicking a button like this
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onCreate(new Bundle());
}
});
If you are trying to change the colors when you navigate from one activity to another then you can probably try wrapping up your code for changing the colors in a function and call that function in onResume() (one of the life cycle methods of an activity). onResume() will be called every time when your activity is loaded.
Is there away to put buttons/imagebuttons on top of the "game-surface"?
I'm experimenting with code from this blog:
https://www.simplifiedcoding.net/android-game-development-tutorial-1/
I need to change direction for objects on the screen with some buttons.
In the tutorial a ship changes direction if the touch screen is pressed anywhere.
I have tried this:
ImageButton b1 = (ImageButton) findViewById(R.id.turn_left);
in order to add the button later to the view with addView, but it returns null.
Maybe becaue setContentView(gameView); set the view of GameView and not
GameActivity?
Any suggestions on how to make this work?
public class GameActivity extends AppCompatActivity {
private GameView gameView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Getting display object
Display display = getWindowManager().getDefaultDisplay();
//Getting the screen resolution into point object
Point size = new Point();
display.getSize(size);
gameView = new GameView(this, size.x, size.y);
//adding it to contentview
setContentView(gameView);
}
GameView
public class GameView extends SurfaceView implements Runnable {
...code...
private void draw() {
//checking if surface is valid
if (surfaceHolder.getSurface().isValid()) {
//locking the canvas
canvas = surfaceHolder.lockCanvas();
//drawing a background color for canvas
canvas.drawColor(Color.BLACK);
//Drawing the player
canvas.drawBitmap(
...code...
draw);
//Unlocking the canvas
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
....code....
#Override
public boolean onTouchEvent(MotionEvent motionEvent) {
switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
...code...
return true;
}
}
XML
xmlns:tools="http://schemas.android.com/tools"
tools:context="package.GameActivity">
<ImageButton
android:id="#+id/turn_left"
android:background="#drawable/left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
You can create custom button class which draw bitmap through canvas.
I have a image with a few buttons on the view. One of the buttons moves the image down. It moves the image down by adding 1 px to the top margin. Here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button1);
final ImageView image = (ImageView) findViewById(R.id.image1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ViewGroup.MarginLayoutParams) image.getLayoutParams()).topMargin += 1;
image.requestLayout();
}
});
}
Now, i want to be able to rotate the image. Just like the code i have now, I want a button, when the button is pressed the image will rotate. But how can i do this?
Bitmap source; //Declare Global
float angle=0; //Declare Global
Button button = (Button) findViewById(R.id.button1);
final ImageView image = (ImageView) findViewById(R.id.image1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
angle+=70;
Bitmap rotatedImage=rotateImage(your_image_source,angle);
img.setImageBitmap(rotatedImage);
}
});
public static Bitmap rotateImage(Bitmap sourceImage, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(sourceImage, 0, 0, sourceImage.getWidth(), sourceImage.getHeight(), matrix, true);
}
You can check
Image Rotation in ImageView (Android)
When click a button rotate image clockwise in android
Try this ,I hope it will helps you .
Set the ImageView's scaleType to matrix
Create a new Matrix object
When click the rotate button, call matrix object's postScale method
Set the Matrix object to the ImageView by call setImageMatrix method
imageView.setScaleType(ImageView.ScaleType.MATRIX);
Matrix matrix = new Matrix();
matrix.setRotate(degree); //you can also translate, scale
imageView.setImageViewMatrix(matrix);
i have some imageviews in my project with this id's: stagebtn_1,stagebtn_2,stagebtn_3 and...
by default all of them points to an image nad works perfectly...
now i want to change picture of some of them inside in a loop with this code:
for (int i=1;i<=3;i++) {
int imageViewId = getResources().getIdentifier("stagebtn_" + i, "id", "com.english.game");
ImageView imageView = (ImageView) findViewById(imageViewId);
imageView.setBackgroundResource(R.drawable.pic1);
}
but it doesnt work in the way that i want, this code doesnt change pictures, instead creates 3 new imageview with new picture...
how i can solve this?
you can use this way
public class MainActivity extends Activity {
ImageView imageView;
Integer[] image = { R.drawable.ic_launcher, R.drawable.tmp,R.drawable.android };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
imageView = (ImageView) findViewById(R.id.img);
for(i=0;i<2;i++){
imageView.setImageResource(image[i]);
}
}
}