How to show image using ImageView in Android

If you want to display an image file on the phone, you can do this: private ImageView mImageView; mImageView = (ImageView) findViewById(R.id.imageViewId); mImageView.setImageBitmap(BitmapFactory.decodeFile(“pathToImageFile”)); If you want to display an image from your drawable resources, do this: private ImageView mImageView; mImageView = (ImageView) findViewById(R.id.imageViewId); mImageView.setImageResource(R.drawable.imageFileId); You’ll find the drawable folder(s) in the project res folder. You … Read more

Mask ImageView with round corner background

The best way is to do it in Canvas using PorterDuff operations and/or Shaders. Let’s say your Bitmap is available and stored in mBitmap. Option 1: Using Shaders. @Override public void onDraw(Canvas canvas) { super.onDraw(canvas); // Load the bitmap as a shader to the paint. final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); final Shader shader = … Read more

How to set an imageView’s image from a string?

if you have the image in the drawable folder you are going about this the wrong way. try something like this Resources res = getResources(); String mDrawableName = “logo_default”; int resID = res.getIdentifier(mDrawableName , “drawable”, getPackageName()); Drawable drawable = res.getDrawable(resID ); icon.setImageDrawable(drawable );

android:load svg file from web and show it on image view

Update: For newer version please checkout the Glide Samples (https://github.com/bumptech/glide/tree/master/samples/svg) – You can use Glide (https://github.com/bumptech/glide/tree/v3.6.0) and AndroidSVG (https://bitbucket.org/paullebeau/androidsvg). There is also a sample from Glide: https://github.com/bumptech/glide/tree/v3.6.0/samples/svg/src/main/java/com/bumptech/svgsample/app Setup GenericRequestBuilder requestBuilder = Glide.with(mActivity) .using(Glide.buildStreamModelLoader(Uri.class, mActivity), InputStream.class) .from(Uri.class) .as(SVG.class) .transcode(new SvgDrawableTranscoder(), PictureDrawable.class) .sourceEncoder(new StreamEncoder()) .cacheDecoder(new FileToStreamDecoder<SVG>(new SvgDecoder())) .decoder(new SvgDecoder()) .placeholder(R.drawable.ic_facebook) .error(R.drawable.ic_web) .animate(android.R.anim.fade_in) .listener(new SvgSoftwareLayerSetter<Uri>()); Use RequestBuilder with … Read more

Deep copy of a Drawable

Finally I succeed! I had similar problem, when I used color filter on my drawable it changed the drawable, its very close to the solution of the other people here, but only this worked for me: Drawable drwNewCopy = dr.getConstantState().newDrawable().mutate();

ImageView rounded corners [duplicate]

SIMPLEST APPROACH: Create an xml file rounded_fg.xml under res/drawable/ folder of your app. The content of rounded_fg.xml is as follows, <?xml version=”1.0″ encoding=”UTF-8″?> <shape xmlns:android=”http://schemas.android.com/apk/res/android” android:innerRadiusRatio=”2″ android:shape=”ring” android:thicknessRatio=”1″ android:useLevel=”false”> <gradient android:type=”radial” android:gradientRadius=”8dp” android:endColor=”@color/white” /> </shape> You can match endColor with ImageView container layout background & gradientRadius may be any value as per your requirements (<=36dp). … Read more

Android image view matrix scale + translate

There’s a convenient method called Matrix.setRectToRect(RectF, RectF, ScaleToFit) to help you here. Matrix m = imageView.getImageMatrix(); RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight); RectF viewRect = new RectF(0, 0, imageView.getWidth(), imageView.getHeight()); m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER); imageView.setImageMatrix(m); That should set the matrix m to have combo of scaling and translate values that is needed to show … Read more