2014-03-07 62 views
0

我有幾頁用分頁文件「res/layout」中的PagerAdapter和一個佈局文件以及「res/layout-land」中的一個佈局文件實現。每個佈局有兩個ImageButtons,其中:圖片按鈕在方向變化時丟失圖像

  • 縱向的imageButton1與橫向的imageButton1具有相同的ID。
  • 縱向的imageButton2與橫向的imageButton2具有相同的ID。

我已設定onClickListener到imageButton1按鈕其中:

  • 從ImageButtons
  • 重新縮放圖像以適合/填充ImageButtons
  • 取的圖像,並重新分配重新縮放的圖片到ImageButtons。

但每當我在模擬器改變方向的圖像/在這些按鈕的圖片迷路,或改變在佈局文件最初指定的圖像和不刷新我分配到按鈕編程的圖片。

PS(例如):我在​​onCreate中分配了一個偵聽器給button1,而且這個偵聽器在這個按鈕中都適用於縱向和橫向。所以這些不是分離的按鈕!!!!!!

問題:我怎樣才能讓它工作,在改變方向時圖像不會丟失?

thx任何幫助提前!

這裏是我的代碼:

佈局畫像文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:id="@+id/table1" 
android:background="@drawable/shape" 
android:gravity="center" 
> 

<!-- Row 1--> 

<LinearLayout 
     android:layout_width="fill_parent" 
     android:orientation="horizontal" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center"> 

    <LinearLayout 
      android:id="@+id/layout11" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="@drawable/shape" > 

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
      android:id="@+id/layout12" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="@drawable/shape" > 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

</LinearLayout> 

佈局景觀文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" 
android:id="@+id/table1" 
android:background="@drawable/shape" 
android:gravity="center" 
> 


<LinearLayout 
     android:layout_width="fill_parent" 
     android:orientation="horizontal" 
     android:layout_height="fill_parent" 
     android:layout_weight="1" 
     android:gravity="center"> 

    <LinearLayout 
      android:id="@+id/layout11" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="@drawable/shape" > 

     <ImageButton 
      android:id="@+id/imageButton1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 

    <LinearLayout 
      android:id="@+id/layout12" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="@drawable/shape" > 

     <ImageButton 
      android:id="@+id/imageButton2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:src="@drawable/ic_launcher" /> 
    </LinearLayout> 


</LinearLayout> 

MyPageAdapter級:

package com.example.Pagercheck; 

import java.util.List; 

import android.support.v4.view.PagerAdapter; 
import android.support.v4.view.ViewPager; 
import android.view.View; 

public class MyPageAdapter extends PagerAdapter 
{ 
    List<View> pages = null; 
    public MyPageAdapter(List<View> pages) 
    { 
     this.pages = pages; 
    } 


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

    } 

    @Override 
    public boolean isViewFromObject(View view, Object object) 
    { 
     return view.equals(object); 
    } 


    @Override 
    public Object instantiateItem(View collection, int position) 
    { 
     View v = pages.get(position); 
     ((ViewPager) collection).addView(v, 0); 
     return v; 
    } 

    @Override 
    public void destroyItem(View collection, int position, Object view) 
    { 
     ((ViewPager) collection).removeView((View) view); 
    } 




    @Override 
    public void finishUpdate(View arg0) { 
    } 

    @Override 
    public void startUpdate(View arg0) { 
    } 


} 

我的MainActivity級:

package com.example.Pagercheck; 

import java.util.ArrayList; 
import java.util.List; 

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Context; 
import android.content.res.Configuration; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.view.ViewPager; 
import android.support.v4.view.ViewPager.SimpleOnPageChangeListener; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnLongClickListener; 
import android.view.ViewGroup; 
import android.view.ViewGroup.LayoutParams; 
import android.view.ViewGroup.MarginLayoutParams; 
import android.view.ViewTreeObserver; 
import android.view.ViewTreeObserver.OnGlobalLayoutListener; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.GridView; 
import android.widget.ImageButton; 
import android.widget.ImageView; 
import android.widget.LinearLayout; 
import android.widget.TableLayout; 
import android.widget.TableRow; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener 
{ 
    private List<View> pages; 
    private MyPageAdapter pagerAdapter; 
    private ViewPager viewPager; 
    private static Context context; //member zum speichern für context für andere Klassen 
    public static Context getContext(){ return context;} //context für andere Klassen zugänglich machen 
    //private Button btn1; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     context = this; //context in member speichern 

     LayoutInflater inflater = LayoutInflater.from(this); 
     pages = new ArrayList<View>(); 

     View page = inflater.inflate(R.layout.page, null);        
     pages.add(page); 

     page = inflater.inflate(R.layout.page, null); 
     pages.add(page); 

     page = inflater.inflate(R.layout.page, null); 
     pages.add(page); 

     page = inflater.inflate(R.layout.page, null); 
     pages.add(page); 

     page = inflater.inflate(R.layout.page, null); 
     pages.add(page); 


     pagerAdapter = new MyPageAdapter(pages); 
     viewPager = new ViewPager(this); 
     viewPager.setAdapter(pagerAdapter); 
     viewPager.setCurrentItem(0);  

     setContentView(viewPager);  

     for (int i_page=0;i_page<pages.size();i_page++) 
     { 
      //Drag-Listener auf ImageButtons: 
      pages.get(i_page).findViewById(R.id.imageButton1).setOnLongClickListener(new MyLongClickListener()); 
      pages.get(i_page).findViewById(R.id.imageButton1).setOnClickListener(this); 
      pages.get(i_page).findViewById(R.id.imageButton2).setOnLongClickListener(new MyLongClickListener()); 


      //Drag-Listener auf LinearLayouts: 
      pages.get(i_page).findViewById(R.id.layout11).setOnDragListener(new MyDragListener()); 
      pages.get(i_page).findViewById(R.id.layout12).setOnDragListener(new MyDragListener()); 


     } 

     super.onCreate(savedInstanceState); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 

     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 


    @Override 
    public void onWindowFocusChanged(boolean hasFocus) 
    { 
     // TODO Auto-generated method stub 
     super.onWindowFocusChanged(hasFocus); 

     //NOTE FOR STACKOVERFLOW 
     //I COMMENTED THIS OUT SO THAT MY IMAGE BUTTONS DOESNT GET LOST AFTER PAGE 2 SO THAT I CAN TEST MY APP PROPERLY WITH ONCLICK: 

// Bitmap bmp_stift=BitmapFactory.decodeResource(getContext().getResources(), R.drawable.stift); 
//  
//  for (int i_page=0;i_page<pages.size();i_page++) 
//  { 
// 
//  ((ImageButton)pages.get(i_page).findViewById(R.id.imageButton1)).setImageBitmap(bmp_stift); 
//  scalePictureToFitButtom((ImageButton)pages.get(i_page).findViewById(R.id.imageButton1));  
//  scalePictureToFitButtom((ImageButton)pages.get(i_page).findViewById(R.id.imageButton2)); 
// 
//  
//  } 


    } 

    public void scalePictureToFitButtom(ImageButton img_btn) 
    { 

     int width=img_btn.getWidth(); 
     int height=img_btn.getHeight(); 

     BitmapDrawable draw=(BitmapDrawable)img_btn.getDrawable(); 
     Bitmap bmp = ((BitmapDrawable)draw).getBitmap();  
     Bitmap resized = Bitmap.createScaledBitmap(bmp, width-40, height-40, true); //bissle schmaler und niedriger damit man noch den Klickeffekt sieht 


     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 
     params.width=width; 
     params.height=height; 
     img_btn.setImageBitmap(resized); 
     img_btn.setLayoutParams(params); 
     pagerAdapter.notifyDataSetChanged(); 
    } 


    @Override 
    public void onClick(View view) 
    { 
     Bitmap bmp_stift=BitmapFactory.decodeResource(getContext().getResources(), R.drawable.stift); 

     for (int i_page=0;i_page<pages.size();i_page++) 
     { 

      ((ImageButton)pages.get(i_page).findViewById(R.id.imageButton1)).setImageBitmap(bmp_stift); 
      scalePictureToFitButtom((ImageButton)pages.get(i_page).findViewById(R.id.imageButton1));  
      scalePictureToFitButtom((ImageButton)pages.get(i_page).findViewById(R.id.imageButton2)); 


     } 



     Toast einToast = Toast.makeText(view.getContext(), "clicked", Toast.LENGTH_SHORT); 
     einToast.show(); 

    } 

} 
+0

嗨maltonic42 ,你可以添加你的解決方案作爲答案,並接受它,請。 – bummi

+0

你可以接受你自己的答案,在答案旁邊打勾。 – bummi

回答

0

UPDATE:

我現在做到了像這樣(例如保存和恢復imageButton1的位圖):

@Override 
protected void onSaveInstanceState(Bundle outState) 
{ 
// TODO Auto-generated method stub 
    super.onSaveInstanceState(outState); 

    BitmapDrawable draw=(BitmapDrawable)((ImageButton)pages.get(0).findViewById(R.id.imageButton1)).getDrawable(); 
    Bitmap bmp = ((BitmapDrawable)draw).getBitmap();  

    outState.putParcelable("IMG_OF_BUTTON1", bmp); 


    super.onSaveInstanceState(outState); 

} 

@Override 
protected void onRestoreInstanceState(Bundle savedInstanceState) 
{ 
// TODO Auto-generated method stub 
super.onRestoreInstanceState(savedInstanceState); 


Bitmap bmp=  savedInstanceState.getParcelable("IMG_OF_BUTTON1"); 
((ImageButton)pages.get(0).findViewById(R.id.imageButton1)).setImageBitmap(bmp); 


}