Rotating A image with a button (Android) - java

I have a image on my view I need a button, that when the button is clicked the image will start rotating on its center point by 1px at a time.
Right now I have another button that moves the image down, by adding 1 px to the top margin. Here is that code:
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();
I just need to basically do the same thing, but rather then moving the image down, i need to rotate it.

put this in your code: image.setRotation(image.getRotation() + 1);
finally your code:
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((ViewGroup.MarginLayoutParams) image.getLayoutParams()).topMargin += 1;
image.setRotation(image.getRotation() + 1);
image.requestLayout();

Related

Image button onclick change image

I'm new to Android studio. I don't know much about Android Studio and Java but I'm trying to learn it.
My question is:
Is there any way to change image of image button and picture at the same time when the image button is clicked like it's in the picture i have attached?
Thank you so much!
(https://i.stack.imgur.com/ogxDs.png)
(https://i.stack.imgur.com/WQCl6.png)
you can do this
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btn.setBackgroundResource(R.drawable.btn_image);
mImageView.setImageResource(R.drawable.image);
}
});
You just need to listen for Click Event and change the image using
setImageResource()
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
imageButton.setImageResource(R.drawable.your_image_change);
}
});
I assume you have a ImageView and a ImageButton. You want to change the image of imageView when you press the button and at the same time change the image of ImageButton.
ImageView myImageView = findViewById(R.id.my_image_view);
ImageButton myImageButton = findViewById(R.id.my_image_button);
myImageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
myImageView.setImageResource(R.drawable.my_imageview_image);
myImageButton.setImageResource(R.drawable.my_imagebutton_image);
}
});
Set a flag if you don't want to change after clicking second time.

Rotating image in Android with a button

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);

ImageButton changing an Image of another ImageButton causes unintended cycle

Aftermath of this: Image for ImageButton doesn't change as intended
When ImageButton of "ibChamp" is clicked, it opens up champions.xml. In that layout, there is an Imagebutton called "ibAnnie". What I want that to do when clicked is, to change the image of "ibChamp", and to switch to that layout. What happened here is that after "ibAnnie" is clicked, it switches to "create_build_page.xml", and in that is a changed picture of "ibChamp". But that only happens for a split second before changing back to the original, unchanged picture, "create_build_page.xml" layout. When I click the back button, it goes to the "create_build_page.xml" layout with the changed picture, but the buttons do not work. I would gladly provide any additional information required.
I'm still not entirely clear on what you're trying to achieve... but if my understanding is correct, this should get you closer:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.champions);
ImageButton ibAnnie = (ImageButton) findViewById(R.id.ibAnnie);
ibAnnie.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
RelativeLayout test = (RelativeLayout) findViewById(R.id.layoutChampions);
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View createView = layoutInflater.inflate(R.layout.create_build_page, null);
test.addView(createView);
ImageButton img = (ImageButton) view.findViewById(R.id.ibChamp);
img.setImageResource(R.drawable.ic_launcher);
img.setTag(R.drawable.ic_launcher); // so you can retrieve it later
img.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// this will set the imageView of ibAnnie
ibAnnie.setImageView((Integer)view.getTag());
// this will remove the selection view from your layout
((RelativeLayout) findViewById(R.id.layoutChampions)).removeChild(createView);
}
});
// this opens another Activity... you don't want that, at least not for what you seem to be trying to do.
/*try {
Intent open = new Intent("android.intent.action.CREATE");
startActivity(open);
} catch (Exception e) {
}*/
}
});
}

PopUp window to be opened to touches within an Imageview

I have a big image which contains different sections. What I want is different PopUps should open when the user touches different sections of the ImageView. For instance, see this Image below:
In this image, what I want is that when the user clicks 1st square, Popup 1 should open, on square 2 popup should open. How to achieve this please.?
Moreover, I want the ImageView still be zoom and pan enabled. Please help.
Hello you can follow my this post . I think my answer will help you ::
popup window from android options menu not working
For you I have edited the code. You can follow in this manner. If button will be Image and replace button with that :
inside onCreate()
Button btn1=(Button) findViewById(R.id.btn1);
Button btn2=(Button) findViewById(R.id.btn2);
btn1.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
initiatePopupWindow1();
}
})
btn2.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
initiatePopupWindow2();
}
})
outside onCreate()
private PopupWindow pwindo1,pwindo2;
private void initiatePopupWindow1() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopupActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,(ViewGroup)
findViewById(R.id.popup_element));
pwindo1 = new PopupWindow(layout, 350, 350, true);
pwindo1.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initiatePopupWindow2() {
try {
// We need to get the instance of the LayoutInflater
LayoutInflater inflater = (LayoutInflater) PopupActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.popup,(ViewGroup)
findViewById(R.id.popup_element));
pwindo2 = new PopupWindow(layout, 350, 350, true);
pwindo2.showAtLocation(layout, Gravity.CENTER, 0, 0);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(cancel_button_click_listener);
} catch (Exception e) {
e.printStackTrace();
}
}
You can use (though it doesn't seem to be the best way of what actually you are trying to achieve but still you can have a single image)
setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
and the event.getX() and event.getY() will give you the coordinates where you are clicking
for example
if(event.getX() == 100 and event.getY() == 100)
{ // show your popup}
But there will be a problem if you have multiple images
you have to know at what positions these boxes will be present, and you might face problem if the image is to zoomed

OnClicklistener not working

I load the image programmatically and set onclicklistener but it's not working if I click the image.
ImageView ImgBook = new ImageView(this);
ImgBook.setImageResource(R.drawable.one);
ImgBook.setClickable(true);
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//exit code
}
});
How to do this?
First figure out if the imageView is registered to listen to onClick events. Put a toast or a Log.d message to find out if the control is going to the onClick method. If you still have issues, refer to the below link.
To set an ImageView programatically, follow this:
Adding image programmatically to RelativeLayout
You need to set View.onClick.... so replace this code
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
//exit code
}
});
with below
ImgBook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("TEST","in onclick");
}
});
try this code..
ImageView ImgBook = new ImageView(this);
ImgBook.setImageResource(R.drawable.ic_launcher);
LinearLayout lyt=new LinearLayout(this);
LinearLayout.LayoutParams Params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Params.setMargins(6, 0, 6, 0);
lyt.addView(ImgBook);
setContentView(lyt);
ImgBook.setClickable(true);
ImgBook.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
}
});

Categories