This is my situation. I have a one imageview that's being place as the background and a second imageview that's being placed on top of it. In the imageview that's being used as the background it rotates so I would like the imageview ontop of it to follow the rotation translation that is done. How would I do that? Essentially what I want is to anchor the second imageview to follow when the other imageview is being rotated. This is the code I have so far:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:clipChildren="false"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="65dp"
android:layout_marginTop="-20dp"
android:id="#+id/unit"
android:src="#drawable/unit"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:id="#+id/button"
android:src="#drawable/button"
/>
/>
private void rotate(int rotation) {
button.setTranslationX(1);
button.setTranslationY(-1);
if(rotation > 35) {
rotation = 35;
}
if(rotation < 0) {
rotation = 0;
}
Matrix matrix = new Matrix();
unit.setScaleType(ImageView.ScaleType.MATRIX);
matrix.postRotate(rotation,
unit.getDrawable().getBounds().width(), unit.getDrawable().getBounds().height());
unit.setImageMatrix(matrix);
}
Have you tried to rotate the Relative Layout instead?
Related
I have assigned a bitmap to a full-screen ImageView with canvas while streaming. Streaming works, the bitmap is shown in ImageView, but the active area of ImageView seems to be smaller than it's actual size. When I set the background color to ImageView, I get this result:
background fills only a small part of marked ImageView...
I assume this is the reason, why all my efforts to scale the bitmap to the size of ImageView do not work.
Here's my layout xml:
<androidx.constraintlayout.widget.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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ImageView
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCD5D5"
android:rotation="90"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:onClick="take_photo"
android:text="#string/take_photo"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Assigning bitmap to ImageView:
public void run() {
if (mLastFrame != null) {
mutableBitmap = mLastFrame.copy(Bitmap.Config.RGB_565, true);
if (moffset == OFFSET) {
mBitmap = Bitmap.createBitmap(iv_heigth, iv_width, Bitmap.Config.RGB_565);
mCameraView.setImageBitmap(mBitmap);
mCanvas = new Canvas(mBitmap);
mCanvas.drawBitmap(mutableBitmap, 0, 0, null);
moffset += OFFSET;
}
}
}
ImageView size iv_width, iv_height is checked in onCreate function as follows:
ViewTreeObserver viewTreeObserver = mCameraView.getViewTreeObserver(); //check imageview size
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
public void onGlobalLayout() {
mCameraView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//do some things
iv_width = mCameraView.getWidth();
iv_heigth = mCameraView.getHeight();
}
});
}
I am totally frustrated, hopefully, someone can help a lonely beginner...
This is due to the rotation that was applied to the ImageView. If you remove the rotation, you will be able to see that the ImageView is back to normal. The height of the Imageview is actually matching parent and the same goes for width as well. However, when it is rotated by 90 degrees, you can see the width as the height of the ImageView and vice versa.
In order to tackle this problem, I would recommend removing the rotation from the xml layout that you applied to the ImageView. You can do that for your bitmap drawable instead. Before setting it into the ImageView, rotate the drawable by 90 degrees and then set it to the view.
Here are a few clever ways of rotating a bitmap by 90 degrees. I am just copying one answer from this post for convenience.
public static Bitmap rotateBitmap(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
I hope that helps!
I have a bunch of gifs of different sizes and I want to align then in a horizontal ListView so that they're all the same height, obviously not necessarily the same width.
We are using https://github.com/koral--/android-gif-drawable
Example:
Gif display xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:paddingLeft="10dp"
android:id="#+id/holder"
android:paddingTop="5dp"
android:paddingBottom="5dp">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<pl.droidsonroids.gif.GifTextureView
android:layout_width="100dp"
android:id="#+id/gif_rec_view"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</RelativeLayout>
Container
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_below="#+id/title1"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:id="#+id/xD">
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/gif_search_1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:background="#e3e3e3"
/>
</RelativeLayout>
Load gif into view:
final GifTextureView textureView = (GifTextureView) convertView.findViewById(R.id.gif_rec_view);
LoadGif gLoad = new LoadGif(_objects.get(position).url, textureView);
gLoad.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[])null);
How would I best solve this?
Tried bunch of stuff.
//textureView.setLayoutParams(new RelativeLayout.LayoutParams(_objects.get(position).width, _objects.get(position).height));
//RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
//params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
//textureView.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
Edit 1:
So looking at this thread: How to scale an Image in ImageView to keep the aspect ratio
using
android:adjustViewBounds="true"
android:scaleType="centerCrop"
worked wonders, however the images cropped like so:
and
android:adjustViewBounds="true"
android:layout_centerInParent="true"
did not work at all.
Okay so this is how I managed to solve it:
Gif display xml
<pl.droidsonroids.gif.GifTextureView
android:layout_width="150dp"
android:layout_height="match_parent"
android:id="#+id/gif_rec_view"
android:layout_centerHorizontal="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
/>
Adapter
/*Absolutely fucking ridiculous*/
textureView.setLayoutParams(new RelativeLayout.LayoutParams(Utils.pxToDp(_objects.get(position).width), Utils.pxToDp(_objects.get(position).height)));
Utils ...
public static int pxToDp(int px)
{
//return (int) (px / Resources.getSystem().getDisplayMetrics().density);
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, px, Resources.getSystem().getDisplayMetrics());
}
Hope this may help some lone wanderer in the future.
I want to set the width and height of an ImageButton in the Java Class, the width of the button should be the width of the display / 4, the height of the button should be the height of the display / 4.
Code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
ImageButton button = (ImageButton) findViewById(R.id.imageButton);
// button.setWidth(width/4)
// button.setHeight(height/4)
}
Apparently there's no method for a button called setWidth() or setHeight(), so how can I accomplish it?
Possible weight is what are you looking for. For example this code will give you four rectangles, everyone takes quarter of a screen
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/black"/>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/white"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_red_dark"/>
<View
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#android:color/holo_blue_bright"/>
</LinearLayout>
</LinearLayout>
You can define weightSum in linearlayout and views will be scaled to be percent of weightSum.
Not letting me comment but if you look in the activity_main.xml, you should find something like this in it.
<ImageButton
android:contentDescription="#+id/imageButton1"
android:id="#+id/imageButton1" />
from here, you can add into this android:layout_width = 50p, where 50p is 50 pixels wide. You can change this to `android:layout_height = 100p, just change the 50 and 100 to numbers of your liking. so, it would look like this after you add them in,
<ImageButton
android:contentDescription="#+id/imageButton1"
android:id="#+id/imageButton1"
android:layout_width ="50p"
android:layout_height ="50p" />
If you want to set the width and height of the ImageView from Java code you can do something like this:
LayoutParams params = button.getLayoutParams();
params.width = width/4;
params.height = height/4;
button.requestLayout();
So I have this animation I have been working on. Essentially, when you click the image all it does is simply animate the image to a new Y coordinate. Note: Keep in mind that my images are all scaled appropriately for mdli, hdpi, xhdpi etc. So I have 2 problems: 1. When I use .setY on an image view, the y coordinate is loaded differently on other phones. 2. When I use both my image and txt animate methods, they both animate to different y coordinates based on different phones/screens. So my question is how do I fix both of these problems so that they are consistent across all screen sizes?
public class MainActivity extends Activity {
final String tag = "My activity";
ImageView mWhatsTheAnswerImg, mInspireMeImg, mHelpMeThink, mTalkToVince;
ObjectAnimator mImageAnimation, mTxtAnimation;
TextView mWhatsTheAnswerText, mInspireMeTxt, mHelpMeThinkTxt, mTalkToVinceTxt;
boolean mWhatsTheAnswerBOOL, mInspireMeBOOL, mHelpMeThinkBOOL, mTalkToVinceBOOL =false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getActionBar();
actionBar.hide();
mInspireMeImg = (ImageView) findViewById(R.id.inspireme);
mInspireMeImg.setY(750);
mInspireMeTxt = (TextView) findViewById(R.id.inspireMeTxt);
mInspireMeTxt.setY(750);
mInspireMeImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mInspireMeBOOL) {
imageAnimate(mInspireMeImg, 750);
txtAnimate(mInspireMeTxt, 750);
Log.w(tag, "DOWN Coordinates: " + mInspireMeImg.getY() + "Txt: " + mInspireMeTxt.getY());
mInspireMeBOOL = false;
} else {
mInspireMeBOOL = true;
imageAnimate(mInspireMeImg, +290);
txtAnimate(mInspireMeTxt, +290);
Log.w(tag, "UP Coordinates: " + mInspireMeImg.getY() + "Txt: " + mInspireMeTxt.getY());
}
}
});
Image animate and txt animate methods:
private void imageAnimate(ImageView img, float YCoordinates){
mImageAnimation =ObjectAnimator.ofFloat(img, View.TRANSLATION_Y,YCoordinates);
Log.w(tag, "IMG Coordinates: " + img.getY());
mImageAnimation.setDuration(1000);
mImageAnimation.start();
}
private void txtAnimate(TextView txt, float YCoordinates){
mTxtAnimation = ObjectAnimator.ofFloat(txt, View.TRANSLATION_Y, YCoordinates);
Log.w(tag, "TXT Coordinates: " + txt.getY());
mTxtAnimation.setDuration(1000);
mTxtAnimation.start();
}
}
Edit: XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.wyatt.avinceandroid.app.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Background"
android:src="#drawable/coachvincebackground"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="1000dp"
android:id="#+id/inspireme"
android:src="#drawable/inspiremescreen"
android:scaleType="fitXY"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/inspireMeTxt"
android:layout_alignTop="#+id/helpMeThink"
android:layout_centerHorizontal="true"
android:layout_marginTop="89dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1000dp"
android:id="#+id/helpMeThink"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp"
android:src="#drawable/helpmethinkscreen"
android:scaleType="fitXY"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/helpMeThinkTxt"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1000dp"
android:id="#+id/whatsTheAnswer"
android:src="#drawable/whatstheanswerscreen"
android:scaleType="fitXY"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/whatsTheAnswerTxt"
android:layout_alignTop="#+id/AskVince"
android:layout_alignLeft="#+id/inspireMeTxt"
android:layout_alignStart="#+id/inspireMeTxt" />
<ImageView
android:layout_width="match_parent"
android:layout_height="1000dp"
android:id="#+id/AskVince"
android:src="#drawable/askvincescreen"
android:scaleType="fitXY"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/askVinceTxt"
android:layout_below="#+id/helpMeThinkTxt"
android:layout_centerHorizontal="true" />
</RelativeLayout>
The setY and other similar methods expect values in pixels. Now, this is exactly where the problem lies. Different phones have screens of different resolutions and so the 750th pixel will be located at different positions on different phones unless two displays are identical.
The solution is simple. Use values in DP:
mInspireMeImg.setY(dpToPx(750));
and keep this method stored somewhere:
public static int dpToPx(float dpValue, Context context){
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics());
}
More about DP unit:
http://developer.android.com/training/multiscreen/screendensities.html
What is the difference between "px", "dp", "dip" and "sp" on Android?
I've two object in android xml, my object locate in horizontal orientation, imageview in left and textview in right,my code look like
<LinearLayout
android:id="#+id/rowFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/img_dilvChat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginRight="10dip"
/>
<TextView
android:id="#+id/txtFileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dip"
android:gravity="left"
android:text="Nama File : test.zip"
android:textSize="10dip" />
</LinearLayout>
i want to switch 2 object when button fire (imageview to right,textview to left)
how i achieve that ?
The following code should do it. Remove the ImageView and add it to the LinearLayout again. Do this indside the OnClick listener.
LinearLayout row = (LinearLayout) findViewById(R.id.rowFile);
ImageView image = (ImageView) findViewById(R.id.img_dilvChat);
row.removeView(image);
row.addView(image);
row.invalidate();
write these in onClickListener of the Button
(findViewById(R.id.imageView)).getLocationOnScreen(posXY);
int imgx = posXY[0];
int imgy = posXY[1];
(findViewById(R.id.textView)).getLocationOnScreen(posXY);
int txtx = posXY[0];
int txty = posXY[1];
(findViewById(R.id.imageView)).setX(txtx);
(findViewById(R.id.imageView)).setY(txty);
(findViewById(R.id.textView)).setX(imgx);
(findViewById(R.id.textView)).setY(imgy);