【问题标题】:I need to store all colors of imageview inside a linkedlist but its not working我需要将图像视图的所有颜色存储在链表中,但它不起作用
【发布时间】:2015-07-24 08:29:38
【问题描述】:

嗨,正如标题提到的那样,我想逐行并将图像的所有颜色存储在我的 imageview 中的链接列表中,因为我不知道大小。但它一直说x必须> 0或y必须> 0,我用谷歌搜索并修复了它,但错误仍然存​​在。

起初我尝试触摸,但后来评论它尝试点击

通过触摸我可以获得我触摸的每个地方的颜色,但存储它仍然会给我带来错误。

public class Encrypt extends Activity {

private static final int SELECTED_PICTURE=1;
ImageView iv;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_encode);
    Button open = (Button) findViewById(R.id.button1);
    iv=(ImageView)findViewById(R.id.imageView1);
    final Button openGallery = (Button) findViewById(R.id.button1);

    openGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, SELECTED_PICTURE);

        }
    });



}

View.OnClickListener onClick1 = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        TextView txt = (TextView) findViewById(R.id.textView);


        iv.buildDrawingCache();
        Bitmap bitmap = iv.getDrawingCache();



        int pixel_old = 0;
        int pixel_new = 0;
        int count = 0;


        LinkedList<String> colors_list = new LinkedList<>();


        //txt.setText( " h: " + color);

        for (int i=iv.getMinimumHeight(); i< iv.getMaxHeight();i++){
            for(int j=iv.getMinimumWidth(); j<iv.getMaxWidth();j++){
                pixel_new= bitmap.getPixel(i,j);
                if(pixel_new != pixel_old){
                    colors_list.add(String.format("%06X",0xFFFFFF & pixel_new));
                    count =0;
                }
                else {

                }
                pixel_old=pixel_new;

            }
        }
        txt.setText(colors_list.get(1)+"   "+colors_list.get(2));

    }
};
/*
   OnTouchListener imgSourceOnTouchListener
        = new OnTouchListener(){

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        TextView txt = (TextView) findViewById(R.id.textView);


        iv.buildDrawingCache();
        Bitmap bitmap = iv.getDrawingCache();


        int x = (int)event.getX();
        int y = (int)event.getY();
        int pixel = bitmap.getPixel(x,y);

        String color = String.format("%06X",0xFFFFFF & pixel);






        txt.setText("p: "+pixel+" h: "+color);

        return true;
    }};
    */


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
        case SELECTED_PICTURE:
            if(resultCode==RESULT_OK){
                Uri uri=data.getData();
                String[]projection={MediaStore.Images.Media.DATA};

                Cursor cursor=getContentResolver().query(uri, projection, null, null, null);
                cursor.moveToFirst();

                int columnIndex=cursor.getColumnIndex(projection[0]);
                String filePath=cursor.getString(columnIndex);
                cursor.close();

                Bitmap yourSelectedImage=BitmapFactory.decodeFile(filePath);
                Drawable d=new BitmapDrawable(yourSelectedImage);

                iv.setBackground(d);
               // iv.setOnTouchListener(imgSourceOnTouchListener);
                iv.setOnClickListener(onClick1);

            }
            break;

        default:
            break;
    }



}

【问题讨论】:

  • 发布您的错误堆栈

标签: java android linked-list imageview


【解决方案1】:

getDrawingCache() 将返回整个View 的位图,包括边框和填充,如果你想获得ImageView 正在使用的图像的颜色,试试这个:

Bitmap  b = ((BitmapDrawable) iv.getDrawable()).getBitmap();
List<String> colors = new ArrayList<String>();

for(int x = 0; x < b.getWidth(); x++){
   for(int y = 0;y < b.getHeight(); y++){
      colors.add(String.format("%06X",0xFFFFFF & b.getPixel(x,y)));
   }
}

另外,getPixel(...) 方法接受 2 个int 参数,第一个是 x 坐标,第二个是 y 坐标,您在代码中反转此坐标,因为 i 是 y 坐标。

【讨论】:

  • 我尝试了您的解决方案,但与使用链接列表略有不同,但是在我打开图像后,我设置了一个循环以在 OpenImage 活动启动时将所有颜色添加到链接列表,它给了我这个错误 android.graphics .Bitmap android.graphics.drawable.BitmapDrawable.getBitmap()' 在空对象引用上
  • @user1808592 似乎iv.getDrawable() 返回null。您的ImageView 是否包含图像,如果包含,该图像是如何设置的(背景或来源)?如果这不起作用,您可以返回使用getDrawingCache()
  • 首先非常感谢我的朋友花时间帮助我其次我使用 setbackground 我从画廊打开图像到 imageview 并使用此代码并要求它在图像完成后直接运行在 OnActivityResult 中打开
  • 我的答案中的代码将获取使用setImageResource(...)setImageDrawable(....) 方法设置的图像。要将背景设为Bitmap,您可以将((BitmapDrawable) iv.getDrawable()).getBitmap(); 替换为((BitmapDrawable) iv.getBackground()).getBitmap();,但如果背景Drawable 不能转换为BitmapDrawable 对象,这也不起作用。最安全的方法是使用iv.getDrawingCache(),但就像我说过的那样,这比图片更能给你带来好处。
  • 似乎与 .getbackground 一起工作得很好你是一个活生生的救星我的朋友谢谢
猜你喜欢
  • 1970-01-01
  • 2012-11-04
  • 1970-01-01
  • 1970-01-01
  • 2021-05-14
  • 2013-04-18
  • 1970-01-01
  • 2022-11-18
  • 1970-01-01
相关资源
最近更新 更多