如何在 Android 中启用水平滚动?

发布于 2024-12-02 06:32:51 字数 3405 浏览 0 评论 0原文

基本上,下面的代码是我用来从远程服务器检索图像的代码。我的问题是,每当我有像 700x700 这样的大图像时,它就会缩小整个图像,直到它只适合屏幕。我真正想要的是,每当我有这样的大图像时,我希望它保持原样,并显示垂直和水平滚动条。这有可能实现吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello" />
 <ProgressBar    
   android:id="@+id/progressBar"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />
 <ImageView
   android:id="@+id/imageView"
   android:visibility="gone"
   android:layout_width="200dp"
   android:layout_height="200dp"/>

</LinearLayout>

然后是我的主要活动 ImageDownload.java

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class ImageDownload extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView mainImageView = (ImageView) findViewById(R.id.imageView);
  ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
  String imageurl = "http://4.bp.blogspot.com/-xRyw7I08tDM/TgNqD2Ij-3I/AAAAAAAAAOU/FGjboxE-u2U/s1600/cute_cat_cute_1-s357x422-47520.jpg";

  ImageDownloadMessageHandler imageDownloadMessageHandler1= new ImageDownloadMessageHandler(progressBar, mainImageView);
  ImageDownlaodThread imageDownlaodThread = new ImageDownlaodThread(imageDownloadMessageHandler1,imageurl);
  imageDownlaodThread.start();

 }

 class ImageDownlaodThread extends Thread {
  ImageDownloadMessageHandler imageDownloadMessageHandler;
  String imageUrl;

  public ImageDownlaodThread(ImageDownloadMessageHandler imageDownloadMessageHandler, String imageUrl) {
   this.imageDownloadMessageHandler = imageDownloadMessageHandler;
   this.imageUrl = imageUrl;
  }

  @Override
  public void run() {
   Drawable drawable = LoadImageFromWebOperations(imageUrl);
   Message message = imageDownloadMessageHandler.obtainMessage(1, drawable);
   imageDownloadMessageHandler.sendMessage(message);
   System.out.println("Message sent");
  }

 }

 class ImageDownloadMessageHandler extends Handler {
  ProgressBar progressBar;
  View imageTextView;

  public ImageDownloadMessageHandler(ProgressBar progressBar, View imageTextView) {
   this.progressBar = progressBar;
   this.imageTextView = imageTextView;
  }

  @Override
  public void handleMessage(Message message) {
   progressBar.setVisibility(View.GONE);
   imageTextView.setBackgroundDrawable(((Drawable) message.obj));
   imageTextView.setVisibility(View.VISIBLE);
  }

 }

 Drawable LoadImageFromWebOperations(String url) {
  Drawable d = null;
  InputStream is = null;
  try {
   is = (InputStream) new URL(url).getContent();
   d = Drawable.createFromStream(is, "src name");
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return d;
 }

}

Basically, the code below is what I am using to retrieve images from a remote server. My problem is, whenever I have large images like 700x700, it shrinks the entire image until it only fits the screen. What I really want is whenever I have large image like that, I want it to stay just the way it is and show a vertical and horizontal scrollbar instead. is this possible to accomplish?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:text="@string/hello" />
 <ProgressBar    
   android:id="@+id/progressBar"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content" />
 <ImageView
   android:id="@+id/imageView"
   android:visibility="gone"
   android:layout_width="200dp"
   android:layout_height="200dp"/>

</LinearLayout>

Then my main activity ImageDownload.java

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class ImageDownload extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView mainImageView = (ImageView) findViewById(R.id.imageView);
  ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar);
  String imageurl = "http://4.bp.blogspot.com/-xRyw7I08tDM/TgNqD2Ij-3I/AAAAAAAAAOU/FGjboxE-u2U/s1600/cute_cat_cute_1-s357x422-47520.jpg";

  ImageDownloadMessageHandler imageDownloadMessageHandler1= new ImageDownloadMessageHandler(progressBar, mainImageView);
  ImageDownlaodThread imageDownlaodThread = new ImageDownlaodThread(imageDownloadMessageHandler1,imageurl);
  imageDownlaodThread.start();

 }

 class ImageDownlaodThread extends Thread {
  ImageDownloadMessageHandler imageDownloadMessageHandler;
  String imageUrl;

  public ImageDownlaodThread(ImageDownloadMessageHandler imageDownloadMessageHandler, String imageUrl) {
   this.imageDownloadMessageHandler = imageDownloadMessageHandler;
   this.imageUrl = imageUrl;
  }

  @Override
  public void run() {
   Drawable drawable = LoadImageFromWebOperations(imageUrl);
   Message message = imageDownloadMessageHandler.obtainMessage(1, drawable);
   imageDownloadMessageHandler.sendMessage(message);
   System.out.println("Message sent");
  }

 }

 class ImageDownloadMessageHandler extends Handler {
  ProgressBar progressBar;
  View imageTextView;

  public ImageDownloadMessageHandler(ProgressBar progressBar, View imageTextView) {
   this.progressBar = progressBar;
   this.imageTextView = imageTextView;
  }

  @Override
  public void handleMessage(Message message) {
   progressBar.setVisibility(View.GONE);
   imageTextView.setBackgroundDrawable(((Drawable) message.obj));
   imageTextView.setVisibility(View.VISIBLE);
  }

 }

 Drawable LoadImageFromWebOperations(String url) {
  Drawable d = null;
  InputStream is = null;
  try {
   is = (InputStream) new URL(url).getContent();
   d = Drawable.createFromStream(is, "src name");
  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  return d;
 }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅一夜美梦 2024-12-09 06:32:52

试试这个:

<ScrollView android:id="@+id/scrollView1"
    android:layout_width="fill_parent" android:layout_height="fill_parent"  xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
    <HorizontalScrollView android:id="@+id/horizontalScrollView1"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/linearLayout2"
             android:layout_width="match_parent" android:layout_height="match_parent"
             android:orientation="horizontal">
            <TextView/>
            <ImageView/>
            <ProgressBar/>
       </LinearLayout>
      </HorizontalScrollView>
      </LinearLayout>
</ScrollView>

这样你就可以同时进行垂直和水平滚动。

Try this:

<ScrollView android:id="@+id/scrollView1"
    android:layout_width="fill_parent" android:layout_height="fill_parent"  xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
    <HorizontalScrollView android:id="@+id/horizontalScrollView1"
    android:layout_width="wrap_content" android:layout_height="wrap_content">
        <LinearLayout android:id="@+id/linearLayout2"
             android:layout_width="match_parent" android:layout_height="match_parent"
             android:orientation="horizontal">
            <TextView/>
            <ImageView/>
            <ProgressBar/>
       </LinearLayout>
      </HorizontalScrollView>
      </LinearLayout>
</ScrollView>

This way you can have both vertical and horizontal scroll.

话少情深 2024-12-09 06:32:52

布局元素 ScrollViewHorizo​​ntalScrollView 就是为此目的而发明的。

The layout element ScrollView and HorizontalScrollView has been invented for this purpose.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文