如何在所有屏幕上正确调整图像视图的大小?

发布于 2024-10-16 23:58:02 字数 1596 浏览 11 评论 0原文

我正在制作迷你书签应用程序。这个想法是用存储在浏览器书签数据库中的网站图标填充相对布局。我从数据库中提取了 blob 图像,并使用 Drawable.createFromStream 方法将它们转换为可绘制对象。我怎样才能正确地调整这些图像视图的大小,因为它们目前非常小?我遇到的另一个问题是相对布局。如何正确地用图标填充水平区域,以便当它到达屏幕末尾时它们会换行?

这是我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ScrollView scrollView = new ScrollView(this);
    RelativeLayout relativeLayout = new RelativeLayout(this);
    ImageView bookmark;

    scrollView.addView(relativeLayout);

    this.startManagingCursor(cursor);
    Uri Bookmarks = Browser.BOOKMARKS_URI;

    cursor = managedQuery(
            Bookmarks, projection, selection, null, Browser.BookmarkColumns.VISITS + " DESC");

    if (cursor.moveToFirst()) {
        int idCounter = 1;
        ByteArrayInputStream blobImage;

        do{
           bookmark = new ImageView(this);
           bookmark.setId(idCounter++);

           blobImage = new ByteArrayInputStream(
                   cursor.getBlob(cursor.getColumnIndex(BookmarkColumns.FAVICON)));

           bookmark.setImageDrawable(
                   Drawable.createFromStream(blobImage, "" + bookmark.getId()));

           RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

           if(idCounter > 1) {
               params.addRule(RelativeLayout.BELOW, bookmark.getId() - 1);
           }

           relativeLayout.addView(bookmark, params);
        } while (cursor.moveToNext());
     } 

    this.setContentView(scrollView);
}

}

I'm making mini bookmarks application. The idea is to populate the relative layout with favicons of sites that are stored in browser's bookmark database. I extracted blob images from the database and converted them to drawables using Drawable.createFromStream method. How can I properly sizeup those imageviews because currently they are very small? Another problem that I have is the relative layout. How can I properly populate the horizontal area with icons so when it reaches end of screen they go in new line?

Here's my code:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ScrollView scrollView = new ScrollView(this);
    RelativeLayout relativeLayout = new RelativeLayout(this);
    ImageView bookmark;

    scrollView.addView(relativeLayout);

    this.startManagingCursor(cursor);
    Uri Bookmarks = Browser.BOOKMARKS_URI;

    cursor = managedQuery(
            Bookmarks, projection, selection, null, Browser.BookmarkColumns.VISITS + " DESC");

    if (cursor.moveToFirst()) {
        int idCounter = 1;
        ByteArrayInputStream blobImage;

        do{
           bookmark = new ImageView(this);
           bookmark.setId(idCounter++);

           blobImage = new ByteArrayInputStream(
                   cursor.getBlob(cursor.getColumnIndex(BookmarkColumns.FAVICON)));

           bookmark.setImageDrawable(
                   Drawable.createFromStream(blobImage, "" + bookmark.getId()));

           RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                   LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

           if(idCounter > 1) {
               params.addRule(RelativeLayout.BELOW, bookmark.getId() - 1);
           }

           relativeLayout.addView(bookmark, params);
        } while (cursor.moveToNext());
     } 

    this.setContentView(scrollView);
}

}

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

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

发布评论

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

评论(1

飘落散花 2024-10-23 23:58:02

您可以通过多种方法来实现您想要做的事情。

但是 ImageView 具有缩放类型,因此可以强制图像放大但保持比例或放大,以便填充整个视图

http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

在填充屏幕方面,您需要确定每行的图标数量或动态决定图标的数量和大小。

There are a number of ways you can approach what you're trying to do.

But ImageViews have a scale type, so the image can be forced to scale up but maintain ratio or scale up so the entire view is filled

http://developer.android.com/reference/android/widget/ImageView.ScaleType.html

In terms of filling the screen, you'll need to determine either the number of icons per row OR dynamically decide the number of them and size them.

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