我想創建一個android應用程序,該應用程序可以讓我顯示全屏圖像,並在上面顯示下一個和上一個按鈕以在它們之間進行更改。android圖像「查看器」應用程序
任何人都可以指向我的一些教程,我可以找到類似的東西說明嗎?
如果沒有,最好的方法來使用圖像到應用程序?我已經嘗試了幾種創建圖像的對象類的方法,並用drawable在每個使用Bitmap Factory返回圖像的情況下實例化它,但這不起作用。
我是一名初學者,可以真正使用參考資料,但無法找到涵蓋此主題的任何有用內容。
我想創建一個android應用程序,該應用程序可以讓我顯示全屏圖像,並在上面顯示下一個和上一個按鈕以在它們之間進行更改。android圖像「查看器」應用程序
任何人都可以指向我的一些教程,我可以找到類似的東西說明嗎?
如果沒有,最好的方法來使用圖像到應用程序?我已經嘗試了幾種創建圖像的對象類的方法,並用drawable在每個使用Bitmap Factory返回圖像的情況下實例化它,但這不起作用。
我是一名初學者,可以真正使用參考資料,但無法找到涵蓋此主題的任何有用內容。
作爲一個新手我自己一直在與此工作,這很簡單。下面是一些代碼(也許有更好的方法,但是這是我想出如何做到這一點):
package com.imageviewexample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class ImageViewExample extends Activity implements OnClickListener {
/** Called when the activity is first created. */
int image_index = 0;
private static final int MAX_IMAGE_COUNT = 3;
private int[] mImageIds = {
R.raw.image1,
R.raw.image2,
R.raw.image3
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnPrevious = (Button)findViewById(R.id.previous_btn);
btnPrevious.setOnClickListener(this);
Button btnNext = (Button)findViewById(R.id.next_btn);
btnNext.setOnClickListener(this);
showImage();
}
private void showImage() {
ImageView imgView = (ImageView) findViewById(R.id.myimage);
imgView.setImageResource(mImageIds[image_index]);
}
public void onClick(View v) {
switch (v.getId()) {
case (R.id.previous_btn):
image_index--;
if (image_index == -1) {
image_index = MAX_IMAGE_COUNT - 1;
}
showImage();
break;
case (R.id.next_btn):
image_index++;
if (image_index == MAX_IMAGE_COUNT) {
image_index = 0;
}
showImage();
break;
}
}
}
這是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"
>
<Button
android:id="@+id/previous_btn"
android:layout_width="124dip"
android:layout_height="wrap_content"
android:text="Previous"
/>
<Button
android:id="@+id/next_btn"
android:layout_width="124dip"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/previous_btn"
android:text="Next"
/>
<ImageView
android:id="@+id/myimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/previous_btn"
/>
</RelativeLayout>
這是一個圖片瀏覽器解決方案與網格視圖的圖片資源,您可以選擇一個圖片,並顯示爲一個單一的圖片。在這個單一的圖片視圖,這是用PictureViewer.java完成,你可以瀏覽投擲資源圖片使用左鍵和右鍵。您也可以返回到使用HelloGridViewActivity.java完成的網格視圖。 ImageAdapter.java用於定義網格視圖資源和方法。希望這有助於:
HelloGridViewActivity.java:
package com.example.hellogridview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class HelloGridViewActivity extends Activity {
private long prev=0,next= 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
prev = ImageAdapter.getPrevItemId(position);
next = ImageAdapter.getNextItemId(position);
showImage(gridview.getAdapter().getItemId(position),position);
}
});
}
private void showImage(long id, int pos){
Intent pictureViewer = new Intent(this, PictureViewer.class);
pictureViewer.putExtra("pictureId",id);
pictureViewer.putExtra("picturePosition", pos);
pictureViewer.putExtra("picturePrevId", prev);
pictureViewer.putExtra("pictureNextId", next);
startActivityForResult(pictureViewer,0);
}
}
ImageAdapter.java:
package com.example.hellogridview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return mThumbIds[position];
}
public static long getPrevItemId(int position) {
if(--position<0) position = 21;
return mThumbIds[position];
}
public static long getNextItemId(int position) {
if(++position>21) position = 0;
return mThumbIds[position];
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private static Integer[] mThumbIds = {
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
}
PictureViewer.java:
package com.example.hellogridview;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class PictureViewer extends Activity {
long picItem,picPrevItem,picNextItem;
int picPosition;
private ImageView m_vwImage;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.picture_layout);
//citanje podataka prosledjenih ovoj aktivnosti
//namera koja je pokrenula ovu aktivnost
Intent i = getIntent();
picItem = i.getLongExtra("pictureId", -1);
picPrevItem = i.getLongExtra("picturePrevId", -1);
picNextItem = i.getLongExtra("pictureNextId", -1);
picPosition = i.getIntExtra("picturePosition", -1);
m_vwImage = (ImageView) findViewById(R.id.imageview);
//menjamo ulazne vrednosti da bi ih takve prosledili kao povratne
Log.i("Nemanja", "picItem" + picItem);
Drawable image = getResources().getDrawable((int)picItem);
m_vwImage.setImageDrawable(image);
Button startButton = (Button) findViewById(R.id.return_button);
startButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
finish();
}
});
Button leftButton = (Button) findViewById(R.id.left_button);
leftButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Drawable imagePrev = getResources().getDrawable((int)picPrevItem);
m_vwImage.setImageDrawable(imagePrev);
if(--picPosition<0) picPosition = 21;
picPrevItem = (ImageAdapter.getPrevItemId(picPosition));
picNextItem = (ImageAdapter.getNextItemId(picPosition));
}
});
Button rightButton = (Button) findViewById(R.id.right_button);
rightButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Drawable imageNext = getResources().getDrawable((int)picNextItem);
m_vwImage.setImageDrawable(imageNext);
if(++picPosition>21) picPosition = 0;
picNextItem = (ImageAdapter.getNextItemId(picPosition));
picPrevItem = (ImageAdapter.getPrevItemId(picPosition));
}
});
}
}
HelloGridView清單:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.hellogridview"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".HelloGridViewActivity"
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=".PictureViewer"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
這很好。讓我瘋狂的是,我的3或4個測試項目基本上以完全相同的方式完成,但他們要麼強行關閉,要麼不以其他方式工作。謝謝你救我脫掉頭髮。我看到的不同於我的部分是onClickListener,你可以爲什麼必須將它設置爲「this」,以便我可以嘗試獲得更完整的理解。再次感謝。 – Hamid 2010-10-04 10:06:38
作爲Android新手,我無法解釋爲什麼它必須設置爲「this」。我認爲*這個「只是引用你的課程或主要活動。就像在上面的例子中,「this」可以替換爲「ImageViewExample.this」,它的工作原理是一樣的。我從閱讀http://developer.android.com/guide/topics/ui/ui-events.html – ShadowGod 2010-10-04 21:59:38
瞭解到上述內容再次感謝。最有幫助和善良。 – Hamid 2010-10-05 17:48:29