I have defined a bitmap in /drawable/app_background_raw as such:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/actual_png_file"
android:tileMode="repeat" />
Now I want two copies of this defined - one as it is unchanged, and one rotated 90 degerees so that I can use it in portrait mode. I can create the rotated one as such:
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="90"
android:toDegrees="90"
android:pivotX="0%"
android:pivotY="0%"
android:drawable="#drawable/app_background_raw">
</rotate>
..but how do I, with least overhead, create an unchanged copy of app_background_raw? I can obviously create another RotationDrawable rotated 0 degrees, but that feels a bit silly. Suggestions?
Update
This was a bad idea for this scenario, as Android seems to scale the bitmap for portait mode before I can rotate, but in any case I'm interested in knowing what the simplest way of creating an alias for a drawable is.
Related
My purpose is to apply a bg to my main view, rotate by xxx degrees and make it repeat in order to fill all the screen, no matter the resolution of the device.
It seems i could make it rotate BUT it doesn't repeat as i wish. I think Android calculate the bg image, then it rotates.
The result is the following. Empty areas on top right and bottom left corners.
and this is the xml generating the bg
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="-20"
android:pivotX="50%"
android:pivotY="50%">
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/cropped_bg"
android:alpha=".20"
android:tileMode="repeat"
android:gravity="center"/>
</rotate>
Can somebody help me?
Is there any xml that i can use to perform something like this ?
I want to have an single ImageView where i show my picture. The picture is "fillparent" that it goes on the whole screen. But i only want to see the pink part normal and all outside the lines i want something like an "alpha = 0.5" or just that it is a little bit less seen than the main.
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="120px" android:right="120px" android:bottom="120px" android:left="120px">
<shape android:shape="rectangle">
<solid android:color="#9fffffff" />
</shape>
</item>
I want to put an xml as foreground to perform this. With an shape of an rectangle and an alpha of 0.5 it works exactly the opposite side. Now i just need something like an inverse rectangle or something.
Thanks for your time.
Hi I have a problem with controlling position of my object on the screen. When I create activity every object on the screen is placed with animation and troubles starting here. Firstly I tried to placed all things with pixel coordinates, which based on height and width of screen. I want to take it from device when activity starts. During the coding I think the better way will be use the % values cause I don't need to think about diferent screen resolution and stuff like this.
The picture shows, how I thought the screen looks with different values,but I'm wrong.I make the assumption, that the layout match to the parent, so it have nearly this same size as the screen
Cause the range of values for % and %p is from -100 to 100, so where is -100%, when I put something like -60%p I can't see it on the screen. I try to play with the object by changing the values and see results, but for example
object represented by
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true"
android:duration="3000"
android:startOffset="7000">
<translate
android:fromXDelta="2600"
android:toXDelta="0%p"
android:fromYDelta="-10"
android:toYDelta="-10%p"
/>
<rotate
android:fromDegrees="359"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
/>
is higher on the screen then object represented by:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true"
android:duration="3000"
android:startOffset="12000">
<rotate
android:fromDegrees="359"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
/>
<translate
android:fromXDelta="-599"
android:toXDelta="40%p"
android:fromYDelta="-499"
android:toYDelta="-10%p"
/>
I think they should be on this same vertical level, because android:toYDelta is the same.
When we are putting the values to xml code, we are placing the right top corner of the object in my opinion.
Please clear the way I think about coordinates of different type, and where are mistakes?
I would be grateful for every answer.
I am trying to animate two layers of a drawable to achieve the effect of the post-Honeycomb indeterminate progress indicator. The XML is very straightforward but it would seem that only one layer will animate when run on platforms prior to Honeycomb.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:drawable="#drawable/abs__spinner_48_outer_holo"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="1080" />
</item>
<item>
<rotate
android:drawable="#drawable/abs__spinner_48_inner_holo"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="720"
android:toDegrees="0" />
</item>
</layer-list>
Is this simply a limitation of these platforms or is there an alternate syntax that I can use (either in general or specifically targetted at pre-API11) to achieve the desired functionality?
There is indeed a platform limitation, although it's not what you might think. The issue is that pre-API11, RotateDrawable had some crude code in it to require that the animation rotate clockwise by checking if toDegrees was greater than fromDegrees; if not, the two were forced equal to each other. If you modified your example to have the second item move in a forward direction (from 0 to 720, or even -720 to 0), both images would animate fine on all platforms; though I realize that defeats the purpose of what you're aiming for.
Take a look at the cached version Google Codesearch has of RotateDrawable.inflate(), which is the 2.3 version of the method used to turn the XML into the object, and you'll see what I mean.
RotateDrawable.java ...the offending code is around line 235...
float fromDegrees = a.getFloat(
com.android.internal.R.styleable.RotateDrawable_fromDegrees, 0.0f);
float toDegrees = a.getFloat(
com.android.internal.R.styleable.RotateDrawable_toDegrees, 360.0f);
toDegrees = Math.max(fromDegrees, toDegrees); //<--There's the culprit
This takes an XML block like the second item that you have there, and turns it into a RotateDrawable that ends up with the same value for fromDegrees and toDegrees (in your case, 720), causing the image to simply stand still. You can visible test this by setting the start value to some value not a multiple of 360 (like 765). You'll see that the image still does not animate, but is rotated to the initial coordinate.
This awkward check was removed in the Honeycomb/ICS sources, which is why you can do backwards rotation on those platforms. Also, it doesn't look like there is a way to set these values from Java code, so a custom RotateDrawableCompat may be in your future :)
HTH
Seems like the quick and dirty solution to get this working in pre honeycomb is to just flip the from and to in the second rotate. This is not ideal but at least the thing spins around (even if it's a bit more "boring"). This is how ABS seems to have solved it.
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:drawable="#drawable/abs__spinner_48_outer_holo"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="1080" />
</item>
<item>
<rotate
android:drawable="#drawable/abs__spinner_48_inner_holo"
android:pivotX="50%"
android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="720" /> <!-- Like this -->
</item>
</layer-list>
Using the layer-list below, my scale drawable is never shown. Why is that?
menu_dialog_header.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape>
<solid android:color="#color/black"></solid>
</shape>
</item>
<item>
<scale
android:scaleHeight="50%"
android:scaleWidth="100%"
android:scaleGravity="top"
android:drawable="#drawable/shine" />
</item>
</layer-list>
shine.xml
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/shine" />
</shape>
menu_dialog.xml
<!-- header -->
<LinearLayout
android:background="#drawable/menu_dialog_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center"
android:padding="8dp"
android:orientation="horizontal" >
...
I have a workaround that may or may not work for you: instead of using <scale>
, try <inset>. You can use a combination of the <item>'s android:top / android:bottom / android:left / android:right and the <inset>'s android:insetXXX to size the image
You can do things like set the sides of each inset value relative to your source image's dimensions. For example, if you have a 64 pixel-wide image and want to cut it down by 50%, you can do (64 / 4 = 16) for each side
It's not exactly the LayerLists's fault. ScaleDrawable additionally depends on the drawable's level, as (briefly) mentioned both in the Drawable Resources and the ScaleDrawable reference.
It seems there is no documentation of how ScaleDrawable will interpret the level.
There seems to be no way to set a drawable's level in XML, which reflects the fact that the primary use case for ScaleDrawable is to animate a drawable's size. Hence, you'll have to set the drawable's level in your code.
I strongly sympathize with your usage of ScaleDrawable here, because it allows for a more powerful way of stacking drawables by being able to position them and define their size.