new size of an image in imageView - java

I would like to know the new size of my image when it is set in an imageview.
I tried this :
System.out.println("Imagesize 1 w:" + imageView.getDrawable().getIntrinsicWidth()+" h "+imageView.getDrawable().getIntrinsicHeight());
System.out.println("Imagesize 2 w:" +loadedImage.getWidth()+ " h " +loadedImage.getHeight());
xml1 of imageView:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
I have this result
Imagesize 1 w:400 h 577
Imagesize 2 w:400 h 577
xml2 of imageView:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Same result:
Imagesize 1 w:400 h 577
Imagesize 2 w:400 h 577
Obviously my image is not displayed in my screen with the same size in case of xml1 and xml2. I guess when I getWidth or getHeight it is the real size of the image, but I would like the size of the image displayed on my screen.
Any ideas?
Update 1:
I forgot to say that loadedImage is the bitmap of my image
Update 2:
xml.
<?xml version="1.0" encoding="utf-8"?>
<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"
android:padding="1dip" >
<ImageView
android:id="#+id/image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:contentDescription="#string/descr_image"
android:layout_alignParentBottom="true" />
<ProgressBar
android:id="#+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>

To get the size of screen objects after they have been created you need to use a view observer like so:
ViewTreeObserver viewTreeObserver = YOURLAYOUT.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
YOURLAYOUT.getViewTreeObserver().removeGlobalOnLayoutListener(this);
//do some things
//YOURLAYOUT.getWidth() should give a correct answer
}
});
}

Related

Scale gifs to fit a horizontal ListView

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.

Button Component Cuts off my custom View

My button component takes space of my custom view class,if i put more it takes up more space,any reason why is that
My oncreate method :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control);
MyCustomPanel view = new MyCustomPanel(this);
DisplayMetrics metrics;
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = getDPI(500, metrics);
int width = getDPI(800,metrics);
layout = (LinearLayout) findViewById(R.id.r2) ;
layout1 = (LinearLayout) findViewById(R.id.l2);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width,
height);
layout.addView(view,params);
}
This is my xml code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/r2"
android:gravity="bottom|start"
android:layout_gravity="bottom|start"
tools:context="company.ciso.com.wheelchair.Control">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:gravity="top|end"
android:layout_gravity="top|end"
android:id="#+id/l2"
>
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/b1"
android:text="S1" />
</LinearLayout>
</LinearLayout>
these are the screen shots
Additionally is there anyway to set gravity of my custom view.??
Update your layout with this code.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/l2"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="top|end"
android:gravity="top|end"
android:orientation="horizontal">
<Button
android:id="#+id/b1"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="S1" />
</LinearLayout>
<LinearLayout
android:id="#+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="horizontal"></LinearLayout>
</RelativeLayout>

Set PercentRelativeLayout percentage in Java

I want to change the width percentage of items inside a PercentRelativeLayoutdynamically in Java based on some events, I found this, but I cannot find where to put the percentages:
mLinerLayout.setLayoutParams(new PercentRelativeLayout.LayoutParams());
My xml looks something like this:
<android.support.percent.PercentRelativeLayout
android:id="#+id/ad_prl_tags"
android:layout_width="match_parent"
android:layout_height="48dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/ad_rv_tags"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
app:layout_widthPercent="70%" />
<!--Add Tags Button-->
<LinearLayout
android:id="#+id/ad_ll_add_tags_container"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/ad_rv_tags">
<ImageView
android:id="#+id/itd_animatable_plus_iv"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<EditText
android:id="#+id/ad_et_add_tags_and_info"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.percent.PercentRelativeLayout>
I want to set the width percentage of the LinearLayout to 70% and the width of the RecyclerView to 30% in Java.
You can use it pragmatically with this method :
public static void setWidthPercent(View view, float width) {
PercentLayoutHelper.PercentLayoutParams params =(PercentLayoutHelper.PercentLayoutParams) view.getLayoutParams();
PercentLayoutHelper.PercentLayoutInfo info = params.getPercentLayoutInfo();
info.widthPercent = width;
}

Layout pixels and Canvas pixels different?

So, I have Framelayout witch has SurfaceView(0), and xml layout(1). My layouts height is 200px(below), and I'm drawing 200px high rectangle for testing. For some wierd reason, layout seems to take up more space tan rectangle. How could I make it so, that layout would be in rectangle, regardless of screen size or density? (I hope this not too confusing...)
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="200px"
android:orientation="horizontal"
android:id="#+id/game_ui
"
style="#style/AppTheme">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="Points"
android:textSize="30sp"
android:gravity="center"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:text="500"
android:textSize="60sp"
android:gravity="center"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.2"
android:text="Time"
android:textSize="30sp"
android:gravity="center"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:text="500"
android:textSize="60sp"
android:gravity="center"/>
</LinearLayout>
Drawing a Rectangle:
Graphics g = game.getGraphics();
g.clearScreen(Color.BLACK);
g.drawRect(0, 0, wildth, 200, Color.GREEN);
//draws 200px high rectangle onto surfaceView
Result is this:
http://tinypic.com/r/sb6h55/8
Do not use hardcoded pixels in your layout or code.
Use density independent pixel (DP) in your layout.
android:layout_height="200dp"
Convert pixels in code to DP
final int height = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
final int width = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
Graphics g = game.getGraphics();
g.clearScreen(Color.BLACK);
g.drawRect(0, 0, width, height, Color.GREEN);
For more information read everything on
http://developer.android.com/guide/practices/screens_support.html#screen-independence

My TextView text is not visible

Despite much fiddling, I am unable to make my TextView visible.
I've set up a string which is supposed to contain the text to be displayed in the TextView, but the text is never seen in the Graphical Layout. Even if I use "android:text="text", and change the size, appearance, etc, nothing changes.
My Java code:
public class MainClass extends Activity {
float goldCount = 0.0f;
ImageView minionClick;
TextView textGoldCount;
String textTotal;
#Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainlayout);
minionClick = (ImageView) findViewById(R.id.minioncentreid);
textGoldCount = (TextView) findViewById(R.id.textviewtop);
textTotal = goldCount + " Gold";
textGoldCount.setText(textTotal);`
My XML code:
`<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayoutid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/mainbackground"
>
<TextView
android:id="#+id/textviewtop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="25">
</TextView>
<ImageView
android:id="#+id/minioncentreid"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="50"
android:contentDescription="#+string/desc"
android:src="#drawable/minioncentrethree"
/>
<TextView
android:id="#+id/textviewbottom"
android:layout_weight="25"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
</TextView>
`
The TextView at the bottom is there to that the image in the middle is centred. And I just deleted it and checked, but it did nothing to solve the problem.
Not sure, but maybe it's related to the values in weight and height attributes. You must to declare the height to 0 as follows:
<TextView
android:id="#+id/textviewtop"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" > // 25 and 50 can be replaced by 1 and 2
<ImageView
android:id="#+id/minioncentreid"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
android:contentDescription="#+string/desc"
android:src="#drawable/minioncentrethree" />
<TextView
android:id="#+id/textviewbottom"
android:layout_weight="1"
android:layout_height="0dip"
android:layout_width="wrap_content" />
Let me know if this works.
I'll also add to Filo's answer that you should use android:weightSum attribute in your LinearLayout which should be 100 (25 + 50 + 25) according to your layout.

Categories