Is there a way to set as android:src an embedded resource image with Xamarin Forms?
You could not set the embedded resource from Forms project for it inside xml layout file via android:src.
android:src is used to set a drawable as the content. May be a reference to another resource, in the form #[+][package:]type/name or a theme attribute in the form ?[package:]type/name. May be a color value, in the form of #rgb, #argb, #rrggbb, or #aarrggbb.
But you could get set the embedded resource in code behind.
A simple ImageView inside the layout. The code in Activity:
SetContentView(Resource.Layout.layout3);
var imagview = FindViewById<ImageView>(Resource.Id.imageView1);
#if __ANDROID__
var resourcePrefix = "App3.";
#endif
// note that the prefix includes the trailing period '.' that is required
var assembly = IntrospectionExtensions.GetTypeInfo(typeof(MainPage)).Assembly;
Stream stream = assembly.GetManifestResourceStream
(resourcePrefix + "tiger_24.png");
Android.Graphics.Bitmap bitmap;
var bitmap2 = BitmapFactory.DecodeStream(stream);
imagview.SetImageBitmap(bitmap2);
Related
I'm trying to use android's VectorDrawable as Image source in NativeScript. Using layout inspector I can see that there is something but no image is displayed. Also there is no error about missing image file, no errors at all.
I couldn't find out whether it's possible or not.
Thanks a lot in advance.
So I somehow figured it out:
let context = application.android.context;
// vector is VectorDrawable name
let logo = context.getResources()
.getIdentifier(this.vector, "drawable", context.getPackageName());
this.el.nativeElement.android.setBackgroundResource(logo);
// Access CSS color to change fill color
let backgroundColor: string = (this.el.nativeElement.backgroundColor)
? this.el.nativeElement.backgroundColor.toString()
: SrcDirective.DEFAULT_COLOR;
let newBackgroundColor: Color = new Color(backgroundColor);
this.el.nativeElement.android.getBackground().setTint(newBackgroundColor.android);
According to documentation here, A png resource should be converted to a BitmapDrawable. However I'm observing a strange behavior wherein a png file which has only black pixels in it is resulting in a crash because of ClassCastException (wrapped in InvocationTargetException) if I try to do the following in a Custom View's constructor :
...
tempDrawable = typedArr.getDrawable(R.styleable.CustomView_src); // Source points to a png file
Log.i("TestPNGToResource", "Canonical Class Name " + tempDrawable.getClass().getCanonicalName());
tempBitmap = ((BitmapDrawable) tempDrawable).getBitmap();
...
I see the following logged on android 2.2 and 2.3
09-24 13:21:37.575: I/TestPNGToResource(532): Canonical Class Name android.graphics.drawable.ColorDrawable
Why is the resource not being converted to a BitmapDrawable?
This is an optimization performed by aapt. It is able to recognize images made of a single color and turn them into, well, a single color instead of a bitmap. This is more space-efficient, improves loading times, etc.
i assume this won't occur for 9-patch images, right? also, when using the image file, will extracting the apk file still show the image file inside it?
anyway, i'm not sure if that helps, but have you tried to check this value:
TypedValue value = a.peekValue(R.styleable.CustomView_src);
then , if it's any of the color types, you can create your own drawable with this color. problem is how to get the size if that's what you get.
however, if in the special case you've shown, you get a reference type, you can probably force it to be BitmapDrawable by just creating the bitmap using BitmapFactory.decodeResource().
I have an xml file that specifies a set of image button names. Also, I would like to specify the image resource id as an attribute of an xml node as shown below:
<button name="close" resLocation="R.drawable.close" />
I am parsing the xml in the code, I would like to set the image background for the dynamically created button using the resLocation attribute. Since resLocation is a string, I cannot convert to a Drawable object directly. Is there a way I can workaround?
You can use get getResources().getIdentifier:
String myResourceId = "close"; // Parsed from XML in your case
getResources().getIdentifier(myResourceId, "drawable", "com.my.package.name");
This would require your XML to be a little different:
<button name="close" resLocation="close" />
If you need to keep the R.type.id format in your XML, then you would just need to parse out the type and id:
String myResourceId = "R.drawable.close";
String[] resourceParts = myResourceId.split("\\.");
getResources().getIdentifier(resourceParts[2], resourceParts[1], "com.my.package.name");
You can try
<button name="close" resLocation="#drawable/close" />
or try
ImageButton imgButton=new ImageButton(this);
imgButton.setImageResource(getResources().getDrawable(R.drawable.close));
I have an image view and a string src. I want to set the imageview source to the string src that I have, but am unable to do so beacuse the method expects an int:
imgview.setImageResource(int);
Since this method takes an int how can I accomplish my goal of using a string?
Each image has a resource-number, which is an integer. Pass this number to "setImageResource" and you should be ok.
Check this link for further information:
http://developer.android.com/guide/topics/resources/accessing-resources.html
e.g.:
imageView.setImageResource(R.drawable.myimage);
To set image cource in imageview you can use any of the following ways. First confirm your image is present in which format.
If you have image in the form of bitmap then use
imageview.setImageBitmap(bm);
If you have image in the form of drawable then use
imageview.setImageDrawable(drawable);
If you have image in your resource example if image is present in drawable folder then use
imageview.setImageResource(R.drawable.image);
If you have path of image then use
imageview.setImageURI(Uri.parse("pathofimage"));
What you are looking for is probably this:
ImageView myImageView;
myImageView = mDialog.findViewById(R.id.image_id);
String src = "imageFileName"
int drawableId = this.getResources().getIdentifier(src, "drawable", context.getPackageName())
popupImageView.setImageResource(drawableId);
Let me know if this was helpful :)
Bitmap picture = BitmapFactory.decodeResource(getResources(),R.drawable.phscale);
apart from above code any other ways to get the bitmap from drawables
If you get your png name as string, you could use the following:
int resID = getResources().getIdentifier("bug", "drawable", "org.anddev.android.testproject");
source
If you would like to have full control over resource bits, you'd better put it either in res/asset or res/raw folders and then get access to them as:
InputStream is=this.getResources().getAssets().open("drawable.png");
//
is=this.getResources().openRawResource(R.raw.myDrawable);
See th link
How to convert a Drawable to a Bitmap?