Android Studio:图像显示在IDE和仿真器中,而不是复制到设备

时间:2015-03-06 21:54:48

标签: android android-studio imageview

问题

我正在开发一个测试应用程序以获得对Android Studio的信心。我创建了一个单活动应用程序,其中包含倒数计时器和牙刷的PNG图像。

代码

package com.ecam.myapplication2;

import android.graphics.Color;
import android.os.CountDownTimer;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final TextView mTextField = (TextView)findViewById(R.id.timeRemainingText);
        final ProgressBar pBar = (ProgressBar)findViewById(R.id.progressBar);
        final ProgressBar pBar2 = (ProgressBar)findViewById(R.id.progressBar2);
        final ImageView toothbrushImage = (ImageView)findViewById(R.id.imageView);
        final long maxTime = 2 * 6 * 1000;
        final long steps = 10;
        new CountDownTimer(maxTime, steps) {

            public void onTick(long millisUntilFinished) {
                long minutesRemaining = millisUntilFinished / 1000 / 60;
                long secondsRemaining = (millisUntilFinished / 1000) % 60;
                long centisecondsRemaining = (millisUntilFinished / 10) % 100;
                float percentCompleted = 100 - (((float)millisUntilFinished) / maxTime) * 100;
                //float msFloat = (float) millisUntilFinished;
                //int progress = (int)(msFloat/maxTime*100);
                //mTextField.setText(progress+"seconds remaining: " + String.format("%.2f", msFloat / 1000));
                mTextField.setText(String.format("%02d:%02d.%02d", minutesRemaining, secondsRemaining, centisecondsRemaining));
                pBar.setProgress((int)percentCompleted);
                pBar2.setProgress(100-(int)percentCompleted);
            }

            public void onFinish() {
                mTextField.setText("done!");
                pBar.setProgress(100);
                pBar2.setProgress(0);
            }
        }.start();

    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

布局

<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"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:weightSum="1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="@string/time_remaining_title"
                android:id="@+id/textView"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_gravity="center_horizontal"
                android:gravity="center"
                android:textColor="#ff5611ff"
                android:textSize="45dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textAppearance="?android:attr/textAppearanceLarge"
                android:text="@string/empty"
                android:id="@+id/timeRemainingText"
                android:layout_below="@+id/textView"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:gravity="center_horizontal" />

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="308dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar"
                android:max="100"
                android:indeterminate="false" />

            <ProgressBar
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="308dp"
                android:layout_height="wrap_content"
                android:id="@+id/progressBar2"
                android:max="100"
                android:indeterminate="false"/>

            <ImageView
                android:layout_width="308dp"
                android:layout_height="50dp"
                android:id="@+id/imageView"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="25dp"
                android:scaleType="fitXY"
                android:src="@drawable/toothbrush"
                android:adjustViewBounds="true" />

        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

它在IDE和模拟器上都很好地显示:

enter image description here

但它没有出现在我的设备上(Nexus 5):

enter image description here

我的设备上没有复制或显示图片的想法吗?

我尝试了什么(感谢您的评论!)

  • adjustViewBounds=true
  • 使用background而不是src,或者使用background和src
  • 重新启动IDE
  • 从APK安装

1 个答案:

答案 0 :(得分:1)

尝试使用较小尺寸的图像,我遇到了完全相同的问题,它对我有用。