android - 位图到android中的imageview

标签 android

当我尝试将位图对象设置到 ImageView 中时出现以下错误。 该行的“imageView2 无法解析为变量”:mImg = (ImageView) findViewById(R.id.(imageView2));

代码:

package com.example.ocr01;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {

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

    //SETTING UP BUTTON AND LISTENER
    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener((OnClickListener) this);
}

public void onClick(View v) {
      // do something when the button is clicked
    }

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




//CONVERTING IMAGE TO BITMAP

/*public static Bitmap getBitmapFromURL(String xxx) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}*/

    void create_bitmap(){
        //creating bitmap
        Bitmap source = BitmapFactory.decodeResource(getResources(),
        R.drawable.image1);
        //calling doGreyScale
        doGreyscale(source);
    }

    public static void doGreyscale(Bitmap src) {
        // constant factors
        final double GS_RED = 0.299;
        final double GS_GREEN = 0.587;
        final double GS_BLUE = 0.114;

        // create output bitmap
        Bitmap bmOut = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        // pixel information
        int A, R, G, B;
        int pixel;

        // get image size
        int width = src.getWidth();
        int height = src.getHeight();

        // scan through every single pixel
        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                // get one pixel color
                pixel = src.getPixel(x, y);
                // retrieve color of all channels
                A = Color.alpha(pixel);
                R = Color.red(pixel);
                G = Color.green(pixel);
                B = Color.blue(pixel);
                // take conversion up to one single value
                R = G = B = (int)(GS_RED * R + GS_GREEN * G + GS_BLUE * B);
                // set new pixel color to output bitmap
                bmOut.setPixel(x, y, Color.argb(A, R, G, B));
            }
        }

        //converting bitmap object to show in imageview2
        ImageView mImg;
        mImg = (ImageView) findViewById(R.id.(imageView2));
        mImg.setImageBitmap(bmOut);

    }

}

相同的 XML:

<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=".MainActivity" >


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:src="@drawable/image1" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/imageView1"
    android:src="@drawable/ic_launcher" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

</RelativeLayout>

注意:我是第一次尝试我的学期项目的开发人员。欢迎任何形式的帮助。

最佳答案

试试这个:

    }

    //converting bitmap object to show in imageview2
    ImageView mImg;
    mImg = (ImageView) findViewById(R.id.imageView2);
    mImg.setImageBitmap(bmOut);

}

而不是这个:

    }

    //converting bitmap object to show in imageview2
    ImageView mImg;
    mImg = (ImageView) findViewById(R.id.(imageView2));
    mImg.setImageBitmap(bmOut);

}

关于android - 位图到android中的imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14744672/

相关文章:

android - 一个 AndroidManifest.xml 中的两个 searchable.xml Activity

Android Market 链接显示两个选项

java - 下载并保存文件

android - 从外部存储加载位图时出现 OutOfMemory 异常

android - 何时在 Fragment 的生命周期中调用 onActivityCreated?

android - 如何播放apk扩展文件中的视频?

android - 为什么 ListFragment 在没有 ListView 标签的情况下工作?

android - 使用 Build.VERSION.SDK_INT 是否会自动添加 READ_PHONE_STATE 权限?

滚动时Android listview末尾有额外空间

Android:分辨率模拟器与设备