如何在RecyclerView.Adapter中获取ImageView的高度

时间:2015-07-10 10:21:14

标签: android android-adapter android-recyclerview

我正在使用using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Threading; using System.Drawing; namespace test { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { DateTime dtStart = DateTime.Now; Bitmap pic = new Bitmap("test.jpg"); Bitmap returnImg = new Bitmap(pic.Width, pic.Height); Graphics g = Graphics.FromImage(pic); Color clr; int[] argb = new int[4]; for (int i = 0; i < pic.Width; i++) // pic.Width = 50 { for (int k = 0; k < pic.Height; k++) // pic.Height = 50 { clr = pic.GetPixel(i, k); argb[0] = clr.A; argb[1] = clr.R; argb[2] = clr.G; argb[3] = clr.B; new Thread(() => { if (k < 50) { //HERE IS THE PROBLEM, INTEGER K STILL GETS THE VALUE 50 ASSIGNED TO IT. g.DrawImage(new Bitmap(getBestPic(argb)), new Point(i, k)); } }).Start(); } } returnImg = new Bitmap(pic.Width, pic.Height, g); returnImg.Save("createdImage.jpg"); } //Get picture that is best suited to replace pixel private string getBestPic(int[] argb) { int numOfpics = 5; int[] currentBest = new int[2]; currentBest[0] = 255; currentBest[1] = 150; for (int i = 0; i < numOfpics; i++) { int compare = compareARGB(getAverageRGB(new Bitmap((i + 1).ToString()+".jpg")), argb); if (compare < currentBest[0]) { currentBest[0] = compare; currentBest[1] = i + 1; } } return currentBest[1].ToString() + ".jpg"; } // smaller the value, closer the camparison private int compareARGB(int[] one, int[] two) { int [] tmp = new int[4]; tmp[0] = Convert.ToInt32(Math.Abs(one[0] - two[0])); tmp[1] = Convert.ToInt32(Math.Abs(one[1] - two[1])); tmp[2] = Convert.ToInt32(Math.Abs(one[2] - two[2])); tmp[3] = Convert.ToInt32(Math.Abs(one[3] - two[3])); return (tmp[0] + tmp[1] + tmp[2] + tmp[3]); } //return int arry with size 4 contaning the argb values private int[] getAverageRGB(Bitmap img) { Color clr; int aplha = 0; int red = 0; int green = 0; int blue = 0; for (int i = 0; i < img.Width; i++) { for (int k = 0; k < img.Height; k++) { clr = img.GetPixel(i, k); aplha += clr.A; red += clr.R; green += clr.G; blue += clr.B; } } aplha = aplha / (img.Width * img.Height); red = red / (img.Width * img.Height); green = green / (img.Width * img.Height); blue = blue / (img.Width * img.Height); int[] re = new int[] {aplha,red,green,blue}; return re; } } } RecyclerView,其中显示StaggeredGridLayoutManagerCardViewImageView。图像应该是方形的,所以在布局资源文件中我设置TextView,现在我需要将高度设置为等于此宽度。我尝试在android:layout_width="match_parent"中执行此操作,在Adapter方法中扩展RecyclerView.Adapter,但getMeasuredWidth始终返回0.我在活动 {中设置适配器到RecyclerView {1}}方法。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

我认为在onResume方法完成时就是这种情况,图像未在UI屏幕上设置,因为它会花费时间。 位图异步框架可能对您有所帮助,例如xutils[UniversalImageLoader][1]。您可以在完全下载位图时进行缩放,也可以从服务器获取图像宽度,也可能有其他解决方案。