我想將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);
}
}