【问题标题】:Passing a drawable between Activities image looks Stretched在活动图像之间传递一个drawable看起来拉伸
【发布时间】:2014-10-23 22:59:34
【问题描述】:

我正在构建一个应用程序,其中有一个带有 imageview 和 textview 的列表视图,当我单击列表项时,我想将名称和图像传递给其他活动。

我已经设法做到了,但是当我在其他活动中传递图像时,图像看起来被拉伸了。如何解决这个问题?

我使用 Android Asset Studio 使我下载的图片可以以各种尺寸使用。

在列表视图中,图像看起来不错,但在其他活动中,图像看起来被拉伸了。

这是我的代码:

ListView lv = (ListView) findViewById(R.id.listView);
        ArrayAdapter<ListaAdapterNext> adapter = new MySpecialAdapter();
        lv.setAdapter(adapter);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(NextActivity.this, ListItemActivity.class);
                intent.putExtra("name", skonakia.get(position).getItemNextName().toString());
                intent.putExtra("icon", skonakia.get(position).getImageNextId());
                intent.putExtra("usage", skonakia.get(position).getItemUsage());
                intent.putExtra("description", skonakia.get(position).getItemDescription());
                startActivity(intent);
            }
        });

这里我将可绘制的 id 发送到其他活动

这是我收到它们的方式:

        TextView tv = (TextView)findViewById(R.id.description);
        TextView usage = (TextView)findViewById(R.id.usage);
        String fetchedText = getIntent().getStringExtra("name");
        String fetchedDescription = getIntent().getStringExtra("description");
        String fetchedUsage = getIntent().getStringExtra("usage");
        ImageView iv = (ImageView)findViewById(R.id.imageViewListItem);
        Drawable image = getResources().getDrawable(getIntent().getIntExtra("icon", -1));
        iv.setImageDrawable(image);
        tv.setText(fetchedDescription);
        usage.setText(fetchedUsage);
        getActionBar().setTitle(fetchedText);

这是我用于列表视图和其他活动的 2 种布局,当有人单击列表项时

ListView 布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/textViewListItem"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="10dp"
        android:layout_gravity="center_horizontal" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_marginTop="-40dp"
        android:id="@+id/imageView"
        android:src="@drawable/ic_app" />
</LinearLayout>

ListItemClicked 布局:

<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"
    tools:context="com.example.kostas.myapplication.ListItemActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:weightSum="1">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:id="@+id/imageViewListItem"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/description"
            android:id="@+id/textView" />

        <TextView
            android:id="@+id/description"
            android:text="@string/hello_world"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="30dp" />

        <View
            android:id="@+id/view1"
            android:layout_width="fill_parent"
            android:layout_height="2dip"
            android:layout_marginTop="20dp"
            android:layout_alignParentLeft="true"
            android:background="#000" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/usage"
            android:id="@+id/textView2"
            android:layout_marginTop="20dp"
            android:layout_marginBottom="-15dp" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:id="@+id/usage"
            android:layout_marginTop="25dp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/buy"
            android:id="@+id/buyButton"
            android:layout_marginTop="50dp" />


    </LinearLayout>
    </ScrollView>

</RelativeLayout>

【问题讨论】:

    标签: android android-layout listview android-intent


    【解决方案1】:

    为什么不将可绘制对象转换为字节数组,通过 put 将字节数组传递给意图,然后将可绘制对象取回

    【讨论】:

    • 是的文件 KostasMatrix...使用位图工厂...我认为它应该可以完成工作...试试看
    【解决方案2】:

    注意:在 JELLY_BEAN 之前,此函数无法正确检索 此处传递的资源 ID 为时的最终配置密度 另一个 Drawable 资源的别名。这意味着如果密度 别名资源的配置与实际不同 资源,返回的 Drawable 的密度不正确, 导致缩放不良。要解决此问题,您可以改为 通过 TypedArray.getDrawable 检索 Drawable。采用 Context.obtainStyledAttributes 与包含资源的数组 用于创建 TypedArray 的 ID。

    getDrawable(int id)

    这是你的问题吗?

    【讨论】:

    • 好的,我会尝试,但我不认为这是问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-30
    • 2017-08-05
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 1970-01-01
    相关资源
    最近更新 更多