Draw on SurfaceView outside onDraw() Event - android

I have a custom class DrawingView which extends SurfaceView. All the drawing is currently happening in the onTouchEvent()-Event. So how can I draw something on my SurfaceView outside the onTouchEvent?
I defined the DrawingView like this in my activity_main.xml:
<com.example.standardbenutzer.adelpath.DrawingView
android:id="#+id/surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerInParent="true"/>
and I tried this in my MainActivity:
DrawingView dV = (DrawingView)findViewById(R.id.surface);
Canvas canv = dV.surfaceHolder.lockCanvas();
Paint paint = new Paint();
paint.setColor(Color.WHITE);
canv.drawPoint(5,5,paint);
dV.surfaceHolder.unlockCanvasAndPost(canv);
But I get:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.standardbenutzer.adelpath/com.example.standardbenutzer.adelpath.MainActivity}: java.lang.NullPointerException
Any tips here how i can draw on a surfaceView outside the onTouchEvent ?
So my DrawingView which extends SurfaceView now implements the SurfaceHolder.Callback interface.
So in the different constructors i add the Callback event like this:
public DrawingView(Context context) {
super(context);
surfaceHolder.addCallback(this);
}
public DrawingView(Context context, AttributeSet attrs){
super(context, attrs);
surfaceHolder.addCallback(this);
}
public DrawingView(Context context, AttributeSet attrs, int defStyle){
super(context, attrs, defStyle);
surfaceHolder.addCallback(this);
}
And in the onSurfaceCreated Event i render the image like this:
#Override
public void surfaceCreated(SurfaceHolder holder){
render(holder);
}
But this is still inside the DrawingView class. How can i move the drawing part outside the DrawingView class to my main activity? E.G. i want to render my image on button click. I still found nothing on google how i can do this. Maybe it isn't the right way to use a surfaceview? Are there alternatives?

Related

Draw a line in Android without setting it as content

Just a newbie here.
Is it possible to draw a line over a layout?
I have an XML file(activity_main) and it contains a layout which is ImageLayout that I took from github. The layout contains an image and several buttons.
What I want to do is to draw a line between two points on the layout. In order to draw a line, people usually create a Draw class and extends View. On the MainActivity, they would setContent the Draw class. I have already setContent my XML file. How would I draw a line from this point?
EDIT:
I heard about the class Path, I think it is better than using onDraw because I would be connecting(drawing lines between) several points in my layout.
Enlighten me about it if you could
Subclass the container ViewGroup and override its dispatchDraw() method. I am providing example of subclassed LinearLayout, however, this would work with any other ViewGroup.
public class MyLinearLayout extends LinearLayout {
private Paint paint;
public MyLinearLayout(Context context) {
super(context);
initPaint();
}
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
initPaint();
}
public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
private void initPaint() {
paint = new Paint();
paint.setColor(Color.RED);
paint.setStrokeWidth(10);
}
#Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
canvas.drawLine(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
}
}
Use this in your layout to do an horizontal line:
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0"/>

How to set scaleType and adjustViewBounds in Custom ImageView

I've created a custom ImageView class, as I wanted to override the onDraw method for my Canvas; however, the XML attributes scaleType="centerInside" and adjustViewBounds="true" are not being inherited by my custom ImageView class. If I switch to the default ImageView, everything works fine - but how can I have it work in my custom Image View?
In the class below, I'm simply retrieving a bitmap from my application class and drawing it to the canvas; however, when using my CustomImageView - the image is massive and not scaled properly. Any idea how to fix this? Thanks!
Custom ImageView XML:
<com.project.app.controls.CustomImageView
android:id="#+id/myImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="centerInside"/>
Custom ImageView Class:
public class CustomImageView extends ImageView {
ProjectApp wApp;
Paint paint;
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
wApp = ProjectApp.createInstance();
paint = new Paint();
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
canvas.drawBitmap(wApp.photoTaken, 0, 0, null);
}
}
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
wApp = ProjectApp.createInstance();
paint = new Paint();
setAdjustViewBounds(true);
setScaleType(ImageView.ScaleType.CENTER_INSIDE);
}
I think you want to use fitXY instead so the image will be bound by the constraints of the CustomImageView's layout params (and its parent in this case).
android:scaleType="fitXY"

Paint over the specific area in Android

I want to draw over the specific Line, Rectangle or Bitmap using Canvas. If i draw over the Bitmap, it will take the square shape empty background also.
So i want to draw over that particular Bitmap area only.
create a bitmap with "bmp1" name from your desire image
create a custom view
create a class and extend View like this
class MyCustomView extends View{
private Rect m_ImageRect;
private Rect m_TextRect ;
//you need these constructor
//you can init paint object or anything on them
public MyCustomView (Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
m_Context = context;
}
public MyCustomView (Context context, AttributeSet attrs)
{
super(context, attrs);
m_Context = context;
}
public MyCustomView (Context context)
{
super(context);
m_Context = context;
}
//then override on draw method
#Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
//here frist create two rectangle
//one for your image and two for text you want draw on it
m_ImageRect = canvas.getClipBounds();
m_TextRect = canvas.getClipBounds();
//it gives you an area that can draw on it,
//the width and height of your rect depend on your screen size device
canvas.drawBitmap(your bitmap(bmp1), null, m_ImageRect , paint);
canvas.save();
canvas.clipRect(m_TextRect);
canvas.drawText("your text", the x position you want to start draw,
the y position you want to start draw, m_paintText);
canvas.restore();
}
}
at the end put the custom view on your layout,and set field on it to send value to view for draw every thing you want
i hope it's help you,if this is not what you want!
post your code so maybe i can help you more
Seems like you need clipping. See exampls: http://www.example8.com/category/view/id/15543 , Understanding Android Canvas Clipping , http://jtomlinson.blogspot.com/2008/10/clipping.html
With clipping you can specify, which regions should be 'editable'.

How to add view as background to the surfaceView?

Hi I am currently working on Game which contains VIEW of visualization of audio frequency effect in background of surfaceView.
The surfaceView contains actual game play.
I posting some code snippets :-
main.xml
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#000" >
<co.ardor.visualizer.VisualizerView
android:id="#+id/visualizerView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</co.ardor.visualizer.VisualizerView>
</FrameLayout>
Visualizer view are created as follows -
public class VisualizerView extends View
{
private static final String TAG = "VisualizerView";
private byte[] mBytes;
private byte[] mFFTBytes;
private Rect mRect = new Rect();
private Visualizer mVisualizer;
private Set<Renderer> mRenderers; // type of Renderer class
private Paint mFlashPaint = new Paint(); // for flash paint
private Paint mFadePaint = new Paint(); // for fade paint
public VisualizerView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs);
init();
}
public VisualizerView(Context context, AttributeSet attrs)
{
this(context, attrs, 0);
}
public VisualizerView(Context context)
{
this(context, null, 0);
}
// Some code regarding to the audio visualization..
}
My VisualizerView running well so how I can add this as background to SurfaceView (Actual running Game).I am stuck on the problem that "How to add VIEW as background of surfaceView?" Or any another better solution for that..
Thx in advance..
One possibility you could have a look at is to retrieve the View's drawing cache Bitmap and add the Bitmap to the SurfaceView, drawn behind your game contents.
Retrieve the Bitmap:
myVisualizerView.setDrawingCacheEnabled(true);
mVisualizerBitmap = Bitmap.createBitmap(myVisualizerView.getDrawingCache());
myVisualizerView.setDrawingCacheEnabled(false);
Draw the Bitmap in SurfaceView's onDraw(...):
canvas.drawBitmap(mVisualizerBitmap, //other params);
I'm not sure this is a great idea in terms of performance if you have to retrieve the bitmap every frame of your game but it could be worth exploring.
try to add them using LinearLayout,
and make them on top of each other using:
android:layout_alignLeft="..."
android:layout_alignTop="..."

Customized View to xml

I am making a game and I don't want the whole canvas to occupy the screen. Is there a way on how to put my class extending SurfaceView into xml? I want to put it to xml so that I can adjust the position and size of the SurfaceView on the screen.
in src:
public class MySurView extends SurfaceView {
public MySurView(Context context) {
super(context);
}
// you must overide this constructor
public MySurView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
in xml:
<package.name.MySurView
android:id="#+id/surview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</package.name.MySurView>

Categories

Resources