2016-11-14 33 views
1

我最近開始在Studio上進行Android開發,並試圖構建一個應用程序,在該應用程序中使用相機單擊圖片並將其顯示在該應用程序的圖庫中。這是一個基本的場景,就像現在所有的照片應用程序 - 一個攝像頭和一個畫廊,以顯示該應用程序本身點擊的圖片。在使用android studio時在gridView中隨意出現的圖像

我設法整合相機,我現在正在畫廊部分工作。我使用網格視圖來進行畫廊活動的佈局,但效果不佳,因爲圖像有點隨意出現。

下面是我對「GridViewAdapter.java」代碼 -

public class GridViewAdapter extends BaseAdapter { 

    // Declare variables 
    private Activity activity; 
    private String[] filepath; 
    private String[] filename; 

    private static LayoutInflater inflater = null; 

    public GridViewAdapter(Activity a, String[] fpath, String[] fname) { 
     activity = a; 
     filepath = fpath; 
     filename = fname; 
     inflater = (LayoutInflater) activity 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

    } 

    public int getCount() { 
     return filepath.length; 

    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     View vi = convertView; 
     if (convertView == null) 
      vi = inflater.inflate(R.layout.grid_item_layout, null); 

     // Locate the ImageView in gridview_item.xml 
     ImageView image = (ImageView) vi.findViewById(R.id.image); 
     //image.setLayoutParams(new GridView.LayoutParams(215, 215)); 
     image.setPadding(8, 8, 8, 8); 

     // Decode the filepath with BitmapFactory followed by the position 
     Bitmap bmp = BitmapFactory.decodeFile(filepath[position]); 

     // Set the decoded bitmap into ImageView 
     image.setImageBitmap(bmp); 
     return vi; 
    } 
} 

我註釋掉image.setLayoutParams()行,因爲它導致我的應用程序崩潰。

這是對應的.xml文件 -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="5dip" > 

    <GridView 
     android:id="@+id/gallery" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:columnWidth="90dp" 
     android:numColumns="3" 
     android:verticalSpacing="10dp" 
     android:horizontalSpacing="10dp" 
     android:stretchMode="spacingWidthUniform" 
     android:gravity="center"/> 

</RelativeLayout> 

是我得到的輸出是 - screenShot of the app

我開始開發僅在本週的應用程序,所以我將是非常感激這裏提供的任何幫助。

謝謝。

編輯

下面是 「grid_item_layout.xml」 的代碼 -

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:padding="5dip" > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

    <TextView 
     android:id="@+id/text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/image" /> 

</RelativeLayout> 
+0

請添加grid_item_layout.xml源代碼也 –

+0

做到@RahulKumar –

回答

0
<GridView 
    android:id="@+id/gallery" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:verticalSpacing="10dp" 
    android:horizontalSpacing="10dp" 
    android:stretchMode="columnWidth" 
    android:numColumns="3"/> 

嘗試。

+1

謝謝@RahulKumar,這只是完美的:) –

相關問題