2017-05-07 104 views
-2

我在每行都有一個textview和imageview的列表視圖,並試圖找出如何在點擊它們時放大圖像。用戶從他們的圖庫中獲取圖像並將其存儲在名稱數據庫中。它們是用於交付目的地的度假村/酒店的地圖。 這一切是如何佈局:Android單擊列表查看展開ImageView

enter image description here

我試圖讓我的應用程序,我可以點擊一個ImageView的,或只是簡單的列表視圖本身,並在中央放大的圖像的屏幕。如果不在屏幕中心放大,我甚至不介意在新的活動中打開放大的圖像。無論哪種方式都很好,我只希望能夠閱讀地圖,所以通過捏縮放來做一些事情會很棒!我從用戶的畫廊獲取圖像並將其存儲在一個列表中,並要求他們輸入酒店/度假村的名稱。然後我將這些信息保存到一個數據庫並在列表視圖中顯示。我將這些圖像剪下來,以縮略圖的形式將它們放入列表視圖中,但我想在點擊時將它們展開以方便閱讀。任何幫助將不勝感激!我的目標是讓它看起來像this問題的答案,但無法弄清楚如何將其優化到我的代碼中。

我的適配器類如下:

public class dataAdapter extends ArrayAdapter<Hotel> { 

    Context context; 
    ArrayList<Hotel> mHotel; 

    public dataAdapter(Context context, ArrayList<Hotel> hotel) 
    { 
     super(context, R.layout.listhotels, hotel); 
     this.context = context; 
     this.mHotel = hotel; 
    } 

    public class Holder 
    { 
     TextView nameFV; 
     ImageView pic; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
     Hotel data = getItem(position); 

     Holder viewHolder; 

     if (convertView == null) 
     { 
      viewHolder = new Holder(); 
      LayoutInflater inflater = LayoutInflater.from(getContext()); 
      convertView = inflater.inflate(R.layout.listhotels, parent, false); 

      viewHolder.nameFV = (TextView) convertView.findViewById(R.id.txtViewer); 
      viewHolder.pic = (ImageView) convertView.findViewById(R.id.imgView); 

      convertView.setTag(viewHolder); 
     } 
     else 
     { 
      viewHolder = (Holder) convertView.getTag(); 
     } 

     viewHolder.nameFV.setText(data.getFName()); 
     viewHolder.pic.setImageBitmap(convertToBitmap(data.getImage())); 

     return convertView; 
    } 

    private Bitmap convertToBitmap(byte[] b) 
    { 
     return BitmapFactory.decodeByteArray(b, 0, b.length); 
    } 
} 

我的代碼,以顯示酒店列表:

@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.display_hotels); 

    lv = (ListView) findViewById(R.id.list1); 

    db = new DatabaseHandler(this); 

    pic = (ImageView) findViewById(R.id.pic); 
    fname = (EditText) findViewById(R.id.txt1); 

    final ArrayList<Hotel> hotels = new ArrayList<>(db.getAllHotels()); 

    data = new dataAdapter(this, hotels); 

    data.sort(new Comparator<Hotel>() 
    { 
     @Override 
     public int compare(Hotel arg0, Hotel arg1) 
     { 
      return arg0.getFName().compareTo(arg1.getFName()); 
     } 
    }); 

    data.notifyDataSetChanged(); 
    lv.setAdapter(data); 

    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 
    { 
     @Override 
     public void onItemClick(AdapterView<?> parent, View v, int position, long id) 
     { 
      Intent i = new Intent(getApplicationContext(), display_full_image.class); 
      startActivity(i); 
     } 
    }); 
} 

而我的主要活動:

public class MapsMainActivity extends AppCompatActivity { 

    private EditText fname; 
    private ImageView pic; 
    private DatabaseHandler db; 
    private String f_name; 
    private ListView lv; 
    private dataAdapter data; 
    private Hotel dataModel; 
    private Bitmap bp; 
    private byte[] photo; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps_main); 

     db = new DatabaseHandler(this); 

     lv = (ListView) findViewById(R.id.list1); 
     pic = (ImageView) findViewById(R.id.pic); 
     fname = (EditText) findViewById(R.id.txt1); 
    } 

    public void buttonClicked(View v) 
    { 
     int id = v.getId(); 

     switch(id) 
     { 
      case R.id.save: 
       if (fname.getText().toString().trim().equals("")) 
       { 
        Toast.makeText(getApplicationContext(), "Name edit text is empty, Enter name", Toast.LENGTH_LONG).show(); 
       } 
       else 
       { 
        addHotel(); 
       } 
       break; 

      case R.id.display: 
       showRecords(); 
       Intent intent = new Intent(getApplicationContext(), display_hotels.class); 
       startActivity(intent); 
       break; 

      case R.id.pic: 
       selectImage(); 
       break; 
     } 
    } 

    public void selectImage() 
    { 
     Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
     photoPickerIntent.setType("image/*"); 
     startActivityForResult(photoPickerIntent, 2); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    { 
     switch(requestCode) 
     { 
      case 2: 
       if(resultCode == RESULT_OK) 
       { 
        Uri chosenImage = data.getData(); 

        if(chosenImage != null) 
        { 
         bp = decodeUri(chosenImage, 400); 
         pic.setImageBitmap(bp); 
        } 
       } 
     } 
    } 

    protected Bitmap decodeUri(Uri selectedImage, int REQUIRED_SIZE) 
    { 
     try 
     { 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o); 

      int width_tmp = o.outWidth, height_tmp = o.outHeight; 
      int scale = 1; 
      while (true) 
      { 
       if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) 
       { 
        break; 
       } 
       width_tmp /= 2; 
       height_tmp /= 2; 
       scale *= 2; 
      } 

      BitmapFactory.Options o2 = new BitmapFactory.Options(); 
      o2.inSampleSize = scale; 
      return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2); 
     } 
     catch (Exception e) 
     { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    private byte[] profileImage(Bitmap b) 
    { 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     b.compress(Bitmap.CompressFormat.PNG, 0, bos); 
     return bos.toByteArray(); 
    } 

    private void getValues() 
    { 
     f_name = fname.getText().toString(); 
     photo = profileImage(bp); 
    } 

    private void addHotel() 
    { 
     getValues(); 

     db.addHotels(new Hotel(f_name, photo)); 
     Toast.makeText(getApplicationContext(), "Saved successfully", Toast.LENGTH_SHORT).show(); 
    } 

    private void showRecords() 
    { 
     final ArrayList<Hotel> hotels = new ArrayList<>(db.getAllHotels()); 

     data = new dataAdapter(this, hotels); 
     data.notifyDataSetChanged(); 

     lv.setOnItemClickListener(new AdapterView.OnItemClickListener() 
     { 
      @Override 
      public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
      { 
       dataModel = hotels.get(position); 
       Toast.makeText(getApplicationContext(), String.valueOf(dataModel.getID()), Toast.LENGTH_SHORT).show(); 
      } 
     }); 
    } 

} 

預先感謝您任何人都可以幫助我。我將不勝感激。這是我的移動應用程序開發課程的最後一個項目,這個課程將在2天內到期,我真的很接近完成它。感謝您的時間。

+0

我不明白,你有,這是非常常見的任務問題,併爲您可以在互聯網上找到很多例子。簡而言之,您可以在列表視圖的項目中顯示圖像的縮略圖。對於整頁你使用完整的圖像。你發佈的代碼太多了。我甚至沒有看它。 – greenapps

+0

你只是想用選定的圖像顯示放大的圖像,當你點擊listItem的圖像時,是嗎? –

+0

@ankit purwar是的,這正是我想要做的。 –

回答

1

使用這種getView()

@Override 
      public View getView(int position, View convertView, ViewGroup parent) 
      { 
       Hotel data = getItem(position); 

       Holder viewHolder; 

       if (convertView == null) 
       { 
        viewHolder = new Holder(); 
        LayoutInflater inflater = LayoutInflater.from(getContext()); 
        convertView = inflater.inflate(R.layout.listhotels, parent, false); 

        viewHolder.nameFV = (TextView) convertView.findViewById(R.id.txtViewer); 
        viewHolder.pic = (ImageView) convertView.findViewById(R.id.imgView); 

        convertView.setTag(viewHolder); 
       } 
       else 
       { 
        viewHolder = (Holder) convertView.getTag(); 
       } 

       viewHolder.nameFV.setText(data.getFName()); 
       viewHolder.pic.setImageBitmap(convertToBitmap(data.getImage())); 
     viewHolder.pic.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 

         Dialog settingsDialog = new Dialog(context); 
         settingsDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 

         RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(500, 500); 
         lp.addRule(RelativeLayout.CENTER_IN_PARENT); 
         ImageView iv = new ImageView(context); 
         iv.setLayoutParams(lp); 
         iv.setImageResource(R.drawable.img3); 
//use in your case iv.setImageBitmap(convertToBitmap(data.getImage())); 
         settingsDialog.addContentView(iv,lp); 
         settingsDialog.show(); 

        } 
       }); 

       return convertView; 
      } 
+0

這正是我需要的。非常感謝你的幫助!! –

+0

@AnonyBoley很高興它幫助,歡呼夥計..! :) –

0

當您啓動新的活動以顯示完整圖像時,只是將意圖額外傳遞給imgeData。並在FullImageActivit getIntent extra中獲取圖像數據並將其顯示在imageView中。 如果要啓用雙指縮放有一個librabry來處理它 check this