如何将图像移动到屏幕上我想要的位置?

时间:2013-02-15 00:31:37

标签: android image drawable

如何将eclipse中的图像(已存在于drawables文件中)移动到屏幕上的特定位置? 这是我试过的代码,不要认为它甚至接近......

这看起来怎么样?

SurfaceHolder surfaceHolder;
SurfaceView surface = new SurfaceView(this);

Rect rect = new Rect(0,0,100,100);

这是在onCreate

    LinearLayout layout = (LinearLayout) findViewById(R.layout.activity_test2);
    surfaceHolder = surface.getHolder();
    layout.addView(surface);

(公众观点在我的主要活动中)

 public void onDraw(Canvas canvas) {

    canvas = surfaceHolder.lockCanvas();

    Bitmap image1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    canvas.drawBitmap(image1, null, rect, null);

    surfaceHolder.unlockCanvasAndPost(canvas);
}

logcat的

02-15 01:42:16.824: W/Trace(2782): Unexpected value from nativeGetEnabledTags: 0
02-15 01:42:16.824: W/Trace(2782): Unexpected value from nativeGetEnabledTags: 0
02-15 01:42:49.993: W/Trace(2782): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.026: W/Trace(2992): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.026: W/Trace(2992): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.026: W/Trace(2992): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.474: W/Trace(2992): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.505: W/Trace(2992): Unexpected value from nativeGetEnabledTags: 0
02-15 01:43:03.544: D/AndroidRuntime(2992): Shutting down VM
02-15 01:43:03.544: W/dalvikvm(2992): threadid=1: thread exiting with uncaught exception (group=0x40a70930)
02-15 01:43:03.733: E/AndroidRuntime(2992): FATAL EXCEPTION: main
02-15 01:43:03.733: E/AndroidRuntime(2992): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test2/com.example.test2.Test2}: java.lang.NullPointerException
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.os.Looper.loop(Looper.java:137)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.app.ActivityThread.main(ActivityThread.java:5039)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at java.lang.reflect.Method.invokeNative(Native Method)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at java.lang.reflect.Method.invoke(Method.java:511)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at dalvik.system.NativeStart.main(Native Method)
02-15 01:43:03.733: E/AndroidRuntime(2992): Caused by: java.lang.NullPointerException
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.view.View.<init>(View.java:3224)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.view.SurfaceView.<init>(SurfaceView.java:181)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at com.example.test2.Test2.<init>(Test2.java:44)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at java.lang.Class.newInstanceImpl(Native Method)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at java.lang.Class.newInstance(Class.java:1319)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
02-15 01:43:03.733: E/AndroidRuntime(2992):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
02-15 01:43:03.733: E/AndroidRuntime(2992):     ... 11 more
02-15 01:43:11.183: I/Process(2992): Sending signal. PID: 2992 SIG: 9

1 个答案:

答案 0 :(得分:0)

您需要使用SurfaceView来保存画布。就像任何其他视图一样,您可以设置LayoutParams。

activity_main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <LinearLayout
        android:id="@+id/myLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    </LinearLayout>
</LinearLayout>

MainActivity.java

package com.sample.androidcrashtest;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.LinearLayout;
import android.widget.FrameLayout.LayoutParams;

public class MainActivity extends Activity{

    Canvas canvas; //Your canvas to draw on
    LinearLayout myLayout; //The layout that holds the surfaceview
    SurfaceView surface;
    SurfaceHolder surfaceHolder;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_main);
        myLayout = (LinearLayout) findViewById(R.id.myLayout);

        LinearLayout.LayoutParams params_surfaceCanvas = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

        surface = new SurfaceView(this);
        surface.setLayoutParams(params_surfaceCanvas);

        //Assign a surfaceholder to the surface
        surfaceHolder = surface.getHolder();

        myLayout.addView(surface);
        canvas = new Canvas();

        //Call on draw method
        onDraw();
    }

    public void onDraw() {
        //Starts a thread
        new Thread(new Runnable() {
            public void run() {
                while(true) {
                    //Loops until surfaceHolder is valid to use
                    if (surfaceHolder.getSurface().isValid()) {
                        Log.i("Drawing","Drawing"); 
                        //Always lock the canvas if you want to draw in surfaceview
                        canvas = surfaceHolder.lockCanvas();

                        Bitmap image1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                        canvas.drawColor(Color.WHITE); //The background color of the canvas
                        canvas.drawBitmap(image1, 100, 100, null);

                        //Don't forget to unlock it after you draw in the surfaceview
                        surfaceHolder.unlockCanvasAndPost(canvas);

                        //breaks the while and end the thread.
                        break;
                    }
                }
            }
        }).start();
    }
}