ورود

View Full Version : مشکل استفاده از Canvas



AliRezaBeytari
چهارشنبه 01 بهمن 1393, 17:10 عصر
سلام دوستان.
من میخوام از کلاس Canvas استفاده کنم و هرچی درون اون مینویسم ، درون یک View نمایش بدم.

محتویات فایل 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" 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">

<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:id="@+id/myPanel" />
</RelativeLayout>



محتویات فایل MainActivity.java :

package com.example.helloworld;

import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends Activity {

public Paint myPaint;
public Canvas myGraphic;
public View myPanel;

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

// Connect between View and myPanel
myPanel = findViewById(R.id.myPanel);

// Configure Paint
myPaint = new Paint();
myPaint.setColor(Color.BLACK);

// Configure Canvas
myGraphic = new Canvas();
myGraphic.drawLine(10, 10, 100, 10, myPaint);

// Set canvas to View
myPanel.draw(myGraphic);
}
}


اما متاسفانه درون View هیچ چیزی نمایش داده نمیشه. فکر کنم کدم مشکل داره. اگر میشه بگید کجاش رو باید درست کنم ؟؟!!
خیلی ممنونم.

AliRezaBeytari
جمعه 03 بهمن 1393, 13:09 عصر
کسی از دوستان نیست که راهنمایی کنه ؟؟!!!

#root#
جمعه 03 بهمن 1393, 13:18 عصر
سلام

قبلاً یه کلاس از View مشتق کردم و این کارا رو تو متد onDraw انجام دادم، روش شما رو امتحان نکردم.

AliRezaBeytari
جمعه 03 بهمن 1393, 14:05 عصر
سلام

قبلاً یه کلاس از View مشتق کردم و این کارا رو تو متد onDraw انجام دادم، روش شما رو امتحان نکردم.


بله ، من هم اون روش رو دیدم.
اما میخوام به این صورت عمل کنم !!!
راهی نداره ؟؟!!!

harani
جمعه 03 بهمن 1393, 15:16 عصر
به جای اینکه یک کلاس جداگونه بسازی . یک کلاس دورنی داخل MainActivity بساز که از View ارث ببره بعد هر کار داینامیکی بخوای میتونی روش انجام بدی .