I have search for ImageView solutions but I cannot perform some code supplied as maybe it done in Android Studio over the Visual Studio.
I want to display image that store in location (Android Resources/Filesystem) into Main.axml and below are the XML for Main:-
<?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:id="@+id/RelativeLay"
tools:context=".MainActivity">
<VideoView
android:id="@+id/zoneVid"
android:visibility="visible"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:src="@android:drawable/ic_menu_gallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/zoneImg" />
</RelativeLayout>
The image currently exist on "drawable" folder with sequence naming "view01"-"view06". OnCreate method, I will read embedded file into dynamic object and split into array as every line will contain every image name, width, height, top and left margin.
Below are the full code of MainActivity.cs file:-
public class MainActivity : Activity//, MediaPlayer.IOnPreparedListener, ISurfaceHolderCallback
{
public static string PACKAGENAME;
ImageView zoneImg;
MediaPlayer zonePlayer;
VideoView zoneVid;
RelativeLayout rl;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
SetContentView(Resource.Layout.Main);
// GET PACKAGE NAME FROM APPLICATION
MainActivity.PACKAGENAME = ApplicationInfo.PackageName;
DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager.DefaultDisplay.GetRealMetrics(displayMetrics);
int height = displayMetrics.HeightPixels;
int width = displayMetrics.WidthPixels;
Assembly assem = this.GetType().Assembly;
var content = (dynamic)null;
using (System.IO.Stream stream = assem.GetManifestResourceStream(assem.GetName().Name + '.' + "SampleZone.txt"))
{
if (stream != null)
{
using (var reader = new StreamReader(stream))
{
content = reader.ReadToEnd();
}
}
}
if (content != null)
{
rl = FindViewById<RelativeLayout>(Resource.Id.RelativeLay);
RelativeLayout.LayoutParams param;
int resourceId;
string[] zone_element = content.Split(new[] { "\r\n", "\r", "\n" }, System.StringSplitOptions.None);
var gridView = new GridLayout(this);
foreach (var zone_line in zone_element)
{
string[] zone_details = zone_line.Split('|');
double widthpercent = Convert.ToDouble(zone_details[1]), heightpercent = Convert.ToDouble(zone_details[2]);
String FileName = zone_details[5].ToString();
String FileExtension = FileName.Substring(FileName.LastIndexOf("."));
switch (FileExtension)
{
case ".png":
case ".bmp":
case ".gif":
case ".webp":
case ".jpeg":
case ".jpg":
{
zoneImg = new ImageView(this);
int virt_width = (int)Math.Round(width * (widthpercent / 100));
int virt_height = (int)Math.Round(height * (heightpercent / 100));
RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(virt_width, virt_height);
layout.SetMargins(Convert.ToInt32(zone_line[3]), Convert.ToInt32(zone_line[4]), 0, 0);
resourceId = (int)typeof(Resource.Drawable).GetField(System.IO.Path.GetFileNameWithoutExtension(FileName)).GetValue(null);
zoneImg.SetImageResource(resourceId);
zoneImg.RequestLayout();
//param = new RelativeLayout.LayoutParams(virt_width, virt_height)
//{
// LeftMargin = Convert.ToInt32(zone_line[3]),
// TopMargin = Convert.ToInt32(zone_line[4])
//};
//zoneImg.LayoutParameters= new LayoutParams(param);
//rl.AddView(zoneImg);
break;
}
case ".avi":
case ".3gp":
case ".webm":
case ".mkv":
case ".mp4":
{
//zoneVid = FindViewById<VideoView>(Resource.Id.zoneVid);
//resourceId = (int)typeof(Resource.Drawable).GetField(System.IO.Path.GetFileNameWithoutExtension(FileName)).GetValue(null);
//var uri = Android.Net.Uri.Parse("android.resource://" + MainActivity.PACKAGENAME + '/' + resourceId);
//zoneVid.SetVideoURI(uri);
//zoneVid.SetMediaController(null);
//int virt_width = (int)Math.Round(width * (widthpercent / 100));
//int virt_height = (int)Math.Round(height * (heightpercent / 100));
//RelativeLayout.LayoutParams rltvparams = new RelativeLayout.LayoutParams(virt_width, virt_height)
//{
// LeftMargin = Convert.ToInt32(zone_line[3]),
// TopMargin = Convert.ToInt32(zone_line[4])
//};
//zoneVid.LayoutParameters = new LayoutParams(rltvparams);
////rl.AddView(zoneVid);
//zoneVid.RequestFocus();
//zoneVid.Visibility = ViewStates.Visible;
//zoneVid.Start();
break;
}
}
}
}
}
}
I have commented on my code as I have try several things since the image is not properly on margin and dimension. And I cannot see the Visual Studio Intellisense function appear on
image.getLayoutParams().height = 100;
imageView.setLayoutParams(params);
.
What I found from some solutions/discussion/tutorial but unable to figure out:-