2014-12-29 27 views
0

我無法找到網上衝浪後的解決方案。 我有兩個活動類:ImageActivity(MAIN)和ChunkImageActivity。 其他類擴展了BaseAdapter,它由ChunkImageActivity調用。 第一個按鈕包含三個按鈕,通過該按鈕我可以選擇ImageView應該分成的部分數量。 當按下三個按鈕之一時,第二個按鈕將被ImageActivity調用。 當意圖啓動ChunkImageActivity時,問題出現了,ChunkImageActivity在setContentView(R.layout.image_grid)中有一個GridView;startActivity意圖崩潰位圖處理5kb圖像

1 ImageActivity

package com.example.laptop.gridsplitter; 


    import java.util.ArrayList; 

    import android.app.Activity; 
    import android.content.Intent; 
    import android.graphics.Bitmap; 
    import android.graphics.drawable.BitmapDrawable; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.view.View.OnClickListener; 
    import android.widget.Button; 
    import android.widget.ImageView; 




public class ImageActivity extends Activity implements View.OnClickListener { 

@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    /* 
    * Here three, four and five are the id's of the buttons declared as 
    * the contents of the sliding drawer. 
    * See main.xml for clarity 
    */ 

    Button b1 = (Button) findViewById(R.id.three); 
    Button b2 = (Button) findViewById(R.id.four); 
    Button b3 = (Button) findViewById(R.id.five); 

    b1.setOnClickListener(this); 
    b2.setOnClickListener(this); 
    b3.setOnClickListener(this); 
    } 

@Override 
    public void onClick(View view){ 

    //chunkNumbers is to tell how many chunks the image should split 
    int chunkNumbers = 0; 

    /* 
    * switch-case is used to find the button clicked 
    * and assigning the actual value to chunkNumbers variable 
    */ 

    switch (view.getId()) { 
     case R.id.three: 
      chunkNumbers = 9 ; 
      break; 
     case R.id.four: 
      chunkNumbers = 16 ; 
      break; 
     case R.id.five: 
      chunkNumbers = 25 ; 
    } 
    //Getting the source image to split 
    ImageView image = (ImageView) findViewById(R.id.source_image); 

    splitImage(image, chunkNumbers); 
} 



private void splitImage(ImageView image, int chunkNumbers) { 

    //For the number of rows and columns of the grid to be displayed 
    int rows,cols; 

    //For height and width of the small image chunks 
    int chunkHeight,chunkWidth; 

    //To store all the small image chunks in bitmap format in this list 
    ArrayList<Bitmap> chunkedImages = new ArrayList<Bitmap>(chunkNumbers); 

    //Getting the scaled bitmap of the source image 
    BitmapDrawable drawable = (BitmapDrawable) image.getDrawable(); 
    Bitmap bitmap = drawable.getBitmap(); 
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, 
    bitmap.getWidth(),bitmap.getHeight(), true); 

    rows = cols = (int) Math.sqrt(chunkNumbers); 
    chunkHeight = bitmap.getHeight()/rows; 
    chunkWidth = bitmap.getWidth()/cols; 

    //xCoord and yCoord are the pixel positions of the image chunks 
    int yCoord = 0; 
    for(int x=0; x<rows; x++){ 
     int xCoord = 0; 
     for(int y=0; y<cols; y++){ 
    chunkedImages.add(Bitmap.createBitmap(scaledBitmap, xCoord, yCoord, chunkWidth, 
    chunkHeight)); 
     xCoord += chunkWidth; 
     } 
     yCoord += chunkHeight; 
    } 

    //Start a new activity to show these chunks into a grid 
    Intent intent = new Intent(ImageActivity.this, ChunkedImageActivity.class); 
    intent.putParcelableArrayListExtra("image chunks", chunkedImages); 
    ImageActivity.this.startActivity(intent); 
} 
} 

我敢肯定這最後的五線及splitImage的代碼正確

** 2. ChunkImageActivity** 

    package com.example.laptop.gridsplitter; 

    import java.util.ArrayList; 
    import java.util.ArrayList; 
    import android.app.Activity; 
    import android.graphics.Bitmap; 
    import android.os.Bundle; 
    import android.widget.GridView; 

//This activity will display the small image chunks into a grid view 


public class ChunkedImageActivity extends Activity { 

    public void onCreate(Bundle bundle){ 

     super.onCreate(bundle); 
     setContentView(R.layout.image_grid); 

     //Getting the image chunks sent from the previous activity 
     ArrayList<Bitmap> imageChunks = getIntent().getParcelableArrayListExtra("image 
     chunks"); 
     //Getting the grid view and setting an adapter to it 
     GridView grid = (GridView) findViewById(R.id.gridview); 
     grid.setAdapter(new ImageAdapter(this, imageChunks)); 
     grid.setNumColumns((int) Math.sqrt(imageChunks.size())); 
     } 
    } 

**3. ImageAdapter** 

    package com.example.laptop.gridsplitter; 
    import java.util.ArrayList; 
    import java.util.ArrayList; 
    import android.content.Context; 
    import android.graphics.Bitmap; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.BaseAdapter; 
    import android.widget.GridView; 
    import android.widget.ImageView; 

//The adapter class associated with the ChunkedImageActivity class 
    public class ImageAdapter extends BaseAdapter { 

    private Context mContext; 
    private ArrayList<Bitmap> imageChunks; 
    private int imageWidth, imageHeight; 

//constructor 
    public ImageAdapter(Context c, ArrayList<Bitmap> images){ 
     mContext = c; 
     imageChunks = images; 
     imageWidth = images.get(0).getWidth(); 
     imageHeight = images.get(0).getHeight(); 
    } 

@Override 
public int getCount() { 
    return imageChunks.size(); 
} 

@Override 
public Object getItem(int position) { 
    return imageChunks.get(position); 
} 

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

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    ImageView image; 
    if(convertView == null){ 
     image = new ImageView(mContext); 

     /* 
     * NOTE: I have set imageWidth - 10 and imageHeight 
     * as arguments to LayoutParams class. 
     * But you can take anything as per your requirement 
     */ 
    image.setLayoutParams(new GridView.LayoutParams(imageWidth - 10 , imageHeight)); 
     image.setPadding(0, 0, 0, 0); 
    }else{ 
     image = (ImageView) convertView; 
    } 
    image.setImageBitmap(imageChunks.get(position)); 
    return image; 
    } 
    } 

** MANIFEST** 

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.laptop.gridsplitter" 
> 

<application 
    android:hardwareAccelerated="true" 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".ImageActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <activity android:name=".ChunkedImageActivity"> 
    <action android:name="android.intent.action.VIEW" /> 
     <action android:name="android.intent.action.MAIN" /> 

     <category android:name="android.intent.category.LAUNCHER" /> 
    </activity> 


</application> 

</manifest> 


    ** LAYOUT** 


     1. main.xml: 

    ' <?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
> 
<ImageView android:src="@drawable/katewinslet" 
    android:id="@+id/source_image" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"/> 

<SlidingDrawer android:id="@+id/split_slider" 
    android:layout_alignParentBottom="true" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:topOffset="230dip" 
    android:handle="@+id/split_image" 
    android:content="@+id/split_numbers"> 

    <Button android:id="@id/split_image" 
     android:text="@string/button_text" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true"/> 

    <LinearLayout android:id="@id/split_numbers" 
     android:orientation="vertical" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <Button android:id="@+id/three" 
      android:text="@string/three" 
      android:clickable="true" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

     <Button android:id="@+id/four" 
      android:text="@string/four" 
      android:clickable="true" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

     <Button android:id="@+id/five" 
      android:text="@string/five" 
      android:clickable="true" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"/> 

    </LinearLayout> 
    </SlidingDrawer> 

'

2. image_grid.xml: 

'<?xml version="1.0" encoding="utf-8"?> 
<GridView xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/gridview" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
android:numColumns="auto_fit"> 
    </GridView>' 

所有我得到了啓動ChunkImageActivity後出現黑屏。我懷疑是處理位圖的問題,因爲它小於50kb的圖像。 你能幫我嗎?

注意:我以前使用過Toast.makeText,我發現直到splitImage都能正常工作。問題伴隨着處理數組列表或者開始新的活動。

+1

你能更好地格式化你的代碼嗎? –

+0

是的,對不起,我希望現在更具可讀性 –

回答

0

您是否嘗試過調試並檢查arraylist是否正確生成並正確傳遞給映像適配器?

+0

不,我沒有。我怎樣才能做到這一點?謝謝您的幫助! –

+0

當在'splitImage'中創建數組列表時,您可以在主活動中設置斷點,並因此在第二個活動中將數組傳遞給圖像適配器時。 – akash93

+0

你的意思是一個布爾值來檢查它是否被創建? –