安卓图标错误
我从教程网站复制了这段代码,因为我正在尝试学习它。但我在 最后一行出现错误
package com.android.test;
import android.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
public class Rotate extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
,其中写入“.icon”时出现错误。它说:“图标无法解析或不是字段。”
int 宽度 = bitmap.getWidth(); int height = bitmap.getHeight();
矩阵矩阵 = new Matrix(); 矩阵.postRotate(90); 位图旋转Bitmap = Bitmap.createBitmap(位图, 0, 0,
宽度, 高度、矩阵、真实); BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);
ImageView imageView = new ImageView(this); imageView.setImageDrawable(bmd); imageView.setScaleType(ScaleType.CENTER); LinearLayout.addView(imageView, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); setContentView(线性布局); } }
I copied this code from a tutorial site because I am trying to learn it. But I am getting an error at
package com.android.test;
import android.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Spinner;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.ImageView.ScaleType;
public class Rotate extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout linearLayout = new LinearLayout(this);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
On this last line, I get an error where ".icon" is written. It says, "icon cannot be resolved or is not a field."
int width = bitmap.getWidth();
int height = bitmap.getHeight();Matrix matrix = new Matrix(); matrix.postRotate(90); Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0,
width,
height, matrix, true);
BitmapDrawable bmd = new BitmapDrawable(rotatedBitmap);ImageView imageView = new ImageView(this); imageView.setImageDrawable(bmd); imageView.setScaleType(ScaleType.CENTER); linearLayout.addView(imageView, new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); setContentView(linearLayout); } }
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的
import
语句中,这意味着它将查找
android.R.drawable.icon
,而不是R.drawable.icon
>。android.R
包含 SDK 中所有资源的 ID。要访问自己的资源,需要去掉using语句,或者手动编写com.your.package.name.R.drawable.icon
In your
import
statements, you haveThat means it'll look in for
android.R.drawable.icon
, rathern thanR.drawable.icon
.android.R
contains the IDs to all the assets from the SDK. To access your own assets, you need to remove the using statement, or manually writecom.your.package.name.R.drawable.icon
图标有时有效,有时无效。将其更改为“ic_launcher”。这一直有效。这是在“res/drawable-?dpi”下找到的文件名。
它是出现在 Android 设备上的图标。
像这样:
.setIcon(R.drawable.ic_launcher)
Icon some times works and some times it does not. Change it to 'ic_launcher'. This works all the time. This is the file name found under 'res/drawable-?dpi'.
It is the icon that appears on the android device.
Like this:
.setIcon(R.drawable.ic_launcher)
这很可能是因为您的 /res/drawable 文件夹中没有名为 icon.png/icon.bmp/icon.jpg 的图像,
因此,将网站上可能存在的图像复制到该文件夹中,应该没问题。
This is very likely because you don't have a image in your /res/drawable folder called icon.png/icon.bmp/icon.jpg
So, copy the image probably on the site in to that folder and it should be fine.
您必须将名为
icon.png
的图像复制到res/drawable
目录中。You have to copy an image called
icon.png
into theres/drawable
directory.