gpt4 book ai didi

android 图像 "viewer"应用程序

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:12:18 25 4
gpt4 key购买 nike

我正在尝试创建一个 android 应用程序,它可以让我全屏显示图像,顶部有下一个和上一个按钮以在它们之间切换。

任何人都可以指点我一些教程,在那里我可以找到关于类似内容的说明吗?

如果没有,将图像导入应用程序的最佳方法是什么?我已经尝试了几种方法,从为图像创建对象类到使用位图工厂在每种方法中使用可绘制对象实例化它以返回图像,但这行不通。

我是 android 的初学者,确实可以使用引用资料,但找不到涵盖该主题的任何有用内容。

最佳答案

作为我自己的新手,我一直在研究这个,它非常简单。这是一些代码(也许有更好的方法,但这是我想出的方法):

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>

关于android 图像 "viewer"应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3850795/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com