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);
Related
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.
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();
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) {
}*/
}
});
}
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
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]);
}
}
}