Android: Show a Canvas on a View

ساخت وبلاگ

Vote count: 0

I am trying to create a Canvas, draw stuff on it, and then show it on a View. My code below runs with no errors, but the Canvas doesn't show. All I see is a blank, black screen.

I know there are other ways to do this example, but what I am actually trying to do is draw a bunch of stuff on the Canvas, show it, draw more stuff on the Canvas, show it, etc....

I am very new to Android. I have tried to look, but haven't been able to find any simple solution [simple for me :)]


MainActivity.java

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //Get screen size. THIS WORKS; when I log the output it's correct. Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); //Create Canvas. Bitmap bmp = Bitmap.createBitmap(size.x, size.y, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bmp); /* I know the Canvas is created, because when I call canvas.getWidth() and canvas.getHeight() I get the correct pixel size of the device; i.e. (same as size.x and size.y respectively). */ canvas.drawColor(Color.WHITE); //Draw other stuff on the canvas. View v = findViewById(R.id.view); v.draw(canvas); /* I know v is working because when I call v.setBackgroundColor(Color.RED); The whole screen is red. */ }
}


activity_main.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="${relativePackage}.${activityClass}" > <View android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/view" />
</RelativeLayout>


I don't know if this is necessary, but just in case:

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="main.stupidbird" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="17" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:persistent="true"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen" android:screenOrientation="landscape" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
</manifest>
asked 1 min ago

back soft...
ما را در سایت back soft دنبال می کنید

برچسب : android canvas on a view, نویسنده : استخدام کار backsoft بازدید : 386 تاريخ : يکشنبه 31 مرداد 1395 ساعت: 8:31