2015-01-13 106 views
-2

我想將imageView的背景更改爲另一個類的圖庫中的圖像。到目前爲止,我的主要活動是使用imageView顯示其中一張圖片,並且我想將該圖片更改爲另一個來自我在其他活動OnClick上創建的圖片。在Android中如何將圖像設置爲不同活動的圖像視圖?

感謝,

這裏的兩個活動:

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".Main"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="@string/welcome" 
    android:id="@+id/textView" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<ImageView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/imageSelected" 
    android:background="@drawable/brien" 
    android:layout_above="@+id/ButtonChangePic" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/changePic" 
    android:id="@+id/ButtonChangePic" 
    android:layout_alignParentBottom="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="146dp" 
    android:onClick="changePicture"/> 

Main.java

package com.gallery.brien.picturegallery; 

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Picture; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 


public class Main extends Activity { 



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


} 




@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 

public void changePicture(View view) { 
    // Open picture gallery activity 
    Intent intent = new Intent(this,PictureGallery.class); 
    startActivity(intent); 

} 

}

畫廊

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 
import android.widget.Toast; 


public class PictureGallery extends Activity { 

//variable array where pictures are store 
Integer[] Profile = {R.drawable.brien, R.drawable.kick, R.drawable.mma, R.drawable.fun, 
     R.drawable.run}; 

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

    // UI of gallery display. Initializes the GridView and ItemView 
    // classes. Then wait for interaction from user. 
    GridView gal = (GridView) findViewById(R.id.gridView); 
    final ImageView imageView = (ImageView) findViewById(R.id.imageSelected); 

    // Calls the ImageAdapter Class. 
    gal.setAdapter(new ImageAdapter(this)); 
    // Calls the onItemClickListener Class 
    gal.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 

     /*    onItemClick Method 
     The onItemClick method has four arguments. 
     AdapterView<?> arg0 - Records where the user touched screen 
     View arg1 - Parameters of the View user touched 
     int arg2 - Integer value that holds the position of the View 
        in the adapter. 
     long arg3 - Determines the row id of the item that was 
        selected by the user. 
     */ 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 

      Toast.makeText(getBaseContext(), "You have selected picture " + (arg2) 
        + " Brien Calloway", Toast.LENGTH_SHORT).show(); 
      imageView.setImageResource(Profile[arg2]); 



     } 
    }); 


} 

public class ImageAdapter extends BaseAdapter{ 

    private Context context; 
    public ImageAdapter(Context c){ 
     context=c; 
    } 

    @Override 
    public int getCount() {return Profile.length;} 

    @Override 
    public Object getItem(int i) { 
     return null; 
    } 

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

    @Override 
    public View getView(int arg0, View arg1, ViewGroup arg2) { 
     ImageView pic = new ImageView(context); 
     pic.setImageResource(Profile[arg0]); 
     pic.setScaleType(ImageView.ScaleType.FIT_XY); 
     pic.setLayoutParams(new GridView.LayoutParams(200,175)); 
     return pic; 

    } 
} 





@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_picture_gallery, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 
} 

回答

0

這聽起來像你對我應該使用這種偏好。您可以將resourceID的int存儲在首選項對象中。加載主要活動時,您將嘗試獲取該首選項值,然後將背景圖像更改爲該首選項值。

在您的圖庫中,當用戶選擇圖像時,您將保存資源ID作爲首選項。

public class MainActivity extends Activity { 
    public static final String PREFS_NAME = "MyPrefsFile"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

    // Restore preferences 
    SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
    int resourceId = settings.getInt("backgroundImage", DEFAULT_VALUE); 

    // Now use that ID to set the background 
} 

.... 

}

public class PictureGallery extends Activity { 
    ... 
    protected void onCreate(Bundle savedInstanceState) { 
     ... 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
      ... 
      SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); 
      SharedPreferences.Editor editor = settings.edit(); 
      editor.putBoolean("backgroundImage", DEFAULT_VALUE); 

      // Commit the edits! 
      editor.commit(); 

約利用共享偏好的更多信息請參見http://developer.android.com/reference/android/content/SharedPreferences.html

編輯:此解決方案將允許選擇持續啓動應用程序的啓動。你將永久改變背景。如果你只是想暫時改變它,那麼作爲另一個答案建議,你想使用startActivityForResult

相關問題