I have splash.png with gradient. But on screen this image looks not so good ...
My simple .apk for this question consists of:
public class TestditherActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
getWindow().setFormat(PixelFormat.RGBA_8888);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/test"/>
</LinearLayout>
and test.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/splash"
android:antialias="true"
android:dither="true" />
splash.png
result
How to apply RGBA_8888 and dither correctly? Where is my mistake?
The next code gives me perfect result in emulator, but not on the real device:
public class TestditherActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap gradient = BitmapFactory.decodeResource(getResources(), R.drawable.splash, options);
findViewById(R.id.linear).setBackgroundDrawable(new BitmapDrawable(gradient));
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
Take a look at this:
Awful background image quality in Android
Or this blog post about dithering in Android: http://android.amberfog.com/?p=247
In your case the problem is in the bitmap xml. I remember reading somewhere that the dither only works if the bitmap has a tileMode set.
Related
Does any one know how can we send an array of images to the 'SubsamplingScaleImageView' class...
It works fine with one, but... I just want to open like 3 or 4 images and swipe between them and I just can't find a way...
Any help would be great!
Here is my (simple) code:
MainActivity:
private int[] myImages;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImages = new int[]{
R.drawable.p01, R.drawable.p02
};
SubsamplingScaleImageView imageView = (SubsamplingScaleImageView)findViewById(R.id.imageView);
imageView.setImage(ImageSource.resource(R.drawable.p01));
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".MainActivity">
<com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</android.support.constraint.ConstraintLayout>
Thank you!
How can i decrease image size when image is falling from top to bottom?
I have developed top to bottom process through below link
Android Layout Animations from bottom to top and top to bottom on ImageView click
But i am facing one problem.How can i decrease size of image when image is falling top to bottom?
Please let me share your idea or link and code.
Please see my requirement in below image.
you can apply scale transformation to that view.
Matrix matrix = new Matrix();
matrix.setScale(valueX, valueY);
view.setTransform(matrix);
For imageView
imageView.setImageMatrix(matrix);
I solved my question.I hope everyone will like it.
1.Create anim/zoom_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/decelerate_interpolator"
android:fillAfter="true">
<scale
android:duration="5000"
android:fromXScale="100%"
android:fromYScale="100%"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="50%"
android:toYScale="50%" />
<translate
android:duration="5000"
android:fromYDelta="10%"
android:toYDelta="50%" />
2.activity_main.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:paddingLeft="10dp"
android:paddingRight="10dp">
<ImageView android:id="#+id/imgvw"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:src="#mipmap/ic_launcher"/>
<Button
android:id="#+id/btnZoomIn"
android:layout_below="#+id/imgvw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Zoom In" android:layout_marginLeft="100dp" />
<Button
android:id="#+id/btnZoomOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/btnZoomIn"
android:layout_toRightOf="#+id/btnZoomIn"
android:text="Zoom Out" />
</RelativeLayout>
3.MainActivity.java file
public class MainActivity extends AppCompatActivity {
private Button btnzIn;
private Button btnzOut;
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnzIn = (Button)findViewById(R.id.btnZoomIn);
btnzOut = (Button)findViewById(R.id.btnZoomOut);
img = (ImageView)findViewById(R.id.imgvw);
/* btnzIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// img.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom_in));
img.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_down));
// img.clearAnimation();
}
});*/
btnzOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// img.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_up));
img.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(),R.anim.zoom_out));
}
});
}
Im trying to implement a ClipDrawable appear by clicking with diferents images
I have this working with a simple image
Code:
MainActivity.java
ImageView imageview = (ImageView) findViewById(R.id.image);
drawable = (ClipDrawable) imageview.getDrawable();
drawable.setLevel(0);
imageview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
drawable.setLevel(drawable.getLevel()+1000);
}
});
activity_main.xml
<ImageView
android:id="#+id/image"
android:src="#drawable/clip_source"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
clip_source.xml
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/tree"
android:clipOrientation="vertical"
android:gravity="bottom"
/>
this is working but for my project I need to change the image displayed with a variable to display diferents images depending of the user input
I only find one way, using diferent files for each ClipDraw
MainActivity.java
if(aa == false){
t = (ClipDrawable) getResources().getDrawable(R.drawable.clip1);
}else{
t = (ClipDrawable) getResources().getDrawable(R.drawable.clip2);
}
setContentView(R.layout.activity_main);
ImageView imageview = (ImageView) findViewById(R.id.image);
imageview.setBackgroundDrawable(back);
imageview.setImageDrawable(t);
t.setLevel(0);
imageview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t.setLevel(t.getLevel()+1000);
}
});
activity_main.xml
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
clip1.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/farm"
android:clipOrientation="vertical"
android:gravity="bottom"
/>
clip2.xml
<?xml version="1.0" encoding="utf-8"?>
<clip xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="#drawable/werehouse"
android:clipOrientation="vertical"
android:gravity="bottom"
/>
this solve my problem but have the disadvantage of need one file for each image, and I will need at least 20
I have a question about the relationship between button size and button background image size.
Following is my code:
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button = (Button) findViewById(R.id.play);
button.setBackgroundResource(R.drawable.bg_start);
final String LOGTAG="TEST_LOG";
/* get image resource size */
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.drawable.bg_start, options);
/*get button size*/
ViewTreeObserver vto = button.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Log.i(LOGTAG, "button size=" + button.getWidth() +"," +button.getHeight());
}
});
Log.i(LOGTAG, "button play image resource size=" + resWidth + "," + resHeight);
}
}
My layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
>
<Button
android:id="#+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/btn_play_bg"
/>
</RelativeLayout>
btn_play_bg file:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
</shape>
After I run the Activity I got output like this:
I/TEST_LOG﹕ button play image resource size=250,250
I/TEST_LOG﹕ button size=333,333
My question is why the button size doesn't equal to the image size. I thought they should be the same.
I created one simple demo project, which does the animation using AnimationDrawable class of android.
Here is my java code:
public class TestAnimationActivity extends Activity {
/** Called when the activity is first created. */
ImageView imgCircleWhite,imgCircleYellow;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imgCircleYellow = (ImageView)findViewById(R.id.imgCircleYellow);
animateState(true, imgCircleYellow);
}
public void animateState(boolean flag,ImageView imageView)
{
imgCircleYellow.setBackgroundResource(R.drawable.animate_circle_sensor_1);
AnimationDrawable yourAnimation = (AnimationDrawable) imageView.getBackground();
if(!flag)
{
//imageView.getAnimation().reset();
imageView.setBackgroundResource(R.drawable.circle_label_white_1);
yourAnimation.stop();
}
else
{
imageView.setBackgroundResource(R.drawable.animate_circle_sensor_1);
yourAnimation = (AnimationDrawable) imageView.getBackground();
yourAnimation.start();
}
}
}
Here is my main.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ImageView android:id="#+id/imgCircleYellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/circle_label_yellow_1"/>
</LinearLayout>
Below is my animate_sensor_circle_1.xml, which I've put in drawable folder to use with:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="#drawable/circle_label_white_2"
android:duration="50"/>
<item
android:drawable="#drawable/circle_label_yellow_1"
android:duration="50"/>
</animation-list>
When I run the app, only the first item in the above xml file gets displayed, but animation does not occur repeatedly as it should done.
Can anyone direct me where I am going wrong?
It is not the right time to call the AnimationDrawable.start(). You should do this after the view initialization has been completed. Try this:
final ImageView imgCircleYellow = (ImageView)findViewById(R.id.imgCircleYellow);
imgCircleYellow.setBackgroundResource(R.drawable.animate_circle_sensor_1);
imgCircleYellow.post(new Runnable() {
#Override
public void run() {
animateState(true, imgCircleYellow);
}
Check this for more information: https://stackoverflow.com/a/5490922/813135