I am trying to write a real time pitch visualization using TextureView. The 
flow of the code I have implemented seems to be okay but I am unable to see 
the pitch contour I am trying to plot.

So, I have created a custom view as ...

public class PitchSurfaceView extends TextureView implements 
TextureView.SurfaceTextureListener { ...}

In the constructor of the view I have set the listener ...

public PitchSurfaceView(Context context, AttributeSet attrs) {
      ...............

setOpaque(false);     
setSurfaceTextureListener(this);
}

Inside my onSurfaceTextureAvailable(), I am doing the following
@Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int 
width, int height) {
        Log.i(TAG, "Width of the texture view: " + width);
        Log.i(TAG, "Height of the texture view: " + height);

        // creating the bitmaps to draw them ....
        if (!isBitmapsCreated) {
            Log.i(TAG, "Drawing bitmaps for the first time.");
            createBitmaps();
            drawMyBitmaps();
            isBitmapsCreated = true;
        }
       * drawOnView();*
    }

The important method here is drawOnView which actually uses the canvas to 
draw on the View bitmap. As for other methods above, I have already tested 
them using a custom View by just extending the View class. They work fine 
there. Here is my implementation of drawOnView()

protected void drawOnView() {
        if(null == mPitchContourGT) {
            Log.e(TAG, "mPitchContourGT is null", new 
NullPointerException());
            return;
        }

        final Canvas canvas = lockCanvas();

        try {
            // clearing the canvas ...
            Log.d("drawOnView", "Before!!");
            canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
            // Drawing the front padding ...
            canvas.drawBitmap(mBitmaps[0], 0, 0, mPainter);
            int pos = mBitmaps[0].getWidth();

            for (int i = 0; i < (mBitmaps.length - 1) * mNumberLoops; i++) {
                // mBitmaps.length - 1 because we have an extra bitmap at 
the beginning ..
                final int index = i % (mBitmaps.length - 1) + 1;
                canvas.drawBitmap(mBitmaps[index], pos, 0, mPainter);
                pos += mBitmaps[index].getWidth();
            }
        } finally {
            unlockCanvasAndPost(canvas);
        }
        Log.d("drawOnView", "After!!");
    }

Here I am first getting lock at the canvas and drawing my earlier created 
bitmaps on the canvas. I see the debug logs that I have put but I cannot 
see the brawn bitmaps on my view. Anything wrong that I am doing?


On Tuesday, October 16, 2012 at 5:31:33 PM UTC+2, Conrad Chapman wrote:
>
> I looked at this more and Romain Guy unsurprisingly got it 100% right.
> My code pasted here is wrong.
> DO NOT add 
> setWillNotDraw(true); in the TextureView and
>  mSurface.updateTexImage(); in the drawing thread.
> They will mess it up!
>
>
> On Thursday, 4 October 2012 17:17:33 UTC+2, Conrad Chapman wrote:
>>
>> Thank you so much Romain for your reply and example. I might be the only 
>> real canvas textureview example on the net. It worked but not quite as you 
>> said. It might be something to do with my class structure as this part of 
>> the program does not have internal classes..
>> I will explain and show a little. Maybe my approach is bad or wrong I 
>> don't know.
>> CLASSES
>> Main Activity
>> spawns
>> - CustomTextureView class extending TextureView
>>               -this spawns the worker thread (it does not extend thread 
>> but just a worker class)
>>
>> the TextureView constructor
>>
>> public CustomTextureView (Context context, AttributeSet attrs) {
>> super(context, attrs);
>> mContext = context;
>> setWillNotDraw(true);
>> setFocusableInTouchMode(true);
>> setFocusable(true);
>> setSurfaceTextureListener(this);
>>
>> }
>> and the listener callback within this class
>> @Override
>> public void onSurfaceTextureAvailable(SurfaceTexture surface, int width,
>> int height) {
>> // TODO Auto-generated method stub
>> isRunning = true;
>> mSurface = surface;
>> mChart.setTextureSurface(surface);//mChart is the worker class/thread
>> mChart.setSurfaceSize(width, height);
>> mChart.Redraw(true);//this does all the canvas drawing on a bitmap for 
>> buffering.
>> invalidate();//this is essential to make it display on 1st run..why?
>> }
>>
>> The worker class spawned from the CustomTextureView and the critical void
>> protected void RenderCanvas(){
>> final Canvas canvas = CustomTextureView .lockCanvas(null);
>>         try {
>>            canvas.drawBitmap(buffBitmap, 0, 0, null);
>>         } finally {
>>         CustomTextureView .unlockCanvasAndPost(canvas);
>>         mSurface.updateTexImage();
>>         }
>> }
>>
>> So I do not work from the main activity and don't understand why I need 
>> to call invalidate() in onSurfaceTextureAvailable for the 1st run to 
>> display properly.
>>
>> Romain Guy you said NOT to call updateTexImage but if I don't my 2d chart 
>> does not update with the new buffBitmap when it is available.
>> this I definitely don't understand????
>>
>> Anyway it works and thanks for putting me on the right track but am i 
>> still doing some android bad?
>>
>> C
>>
>> On Thursday, 4 October 2012 00:06:00 UTC+2, Romain Guy (Google) wrote:
>>>
>>> Here is a fully working example of Canvas & TextureView: 
>>>
>>> http://pastebin.com/J4uDgrZ8 
>>>
>>> On Wed, Oct 3, 2012 at 3:01 PM, Romain Guy <roma...@android.com> wrote: 
>>> > The problem is that you are calling updateTexImage() yourself. Do 
>>> > *not* do this. You are interfering with TextureView, preventing it 
>>> > from receiving the events that it uses to refresh the screen. 
>>> > 
>>> > On Tue, Oct 2, 2012 at 7:40 AM, Conrad Chapman <chapman...@gmail.com> 
>>> wrote: 
>>> >> Also asked in StackOverflow here 
>>> >> 
>>> http://stackoverflow.com/questions/12688409/android-textureview-canvas-drawing-problems
>>>  
>>> >> 
>>> >> I have an app that used SurfaceView to draw dynamic 2D graphs. It 
>>> worked ok 
>>> >> but transormations etc as we know are not supported. So I went to 
>>> >> TextureView. My old code used another class/thread to do the drawing 
>>> via the 
>>> >> Surfaceholder.lockCanvas(); So I changed this to 
>>> TextureView.lockcanvas. 
>>> >> When this runs the canvas is not accelerated (the view is). It does 
>>> not 
>>> >> display initially but if I touch the screen it displays??? The touch 
>>> is 
>>> >> handled by the main activity. 
>>> >> 
>>> >> Obviously it works as it will display eventually but why doesn't it 
>>> display 
>>> >> immediately? 
>>> >> 
>>> >> The TextureView class implements SurfaceTextureListener as such 
>>> below. 
>>> >> @Override 
>>> >> public void onSurfaceTextureAvailable(SurfaceTexture surface, int 
>>> width, 
>>> >> int height) { 
>>> >> // TODO Auto-generated method stub 
>>> >> isRunning = true; 
>>> >> mySurface = surface; 
>>> >> mChart.setTextureSurface(surface); 
>>> >> mChart.setSurfaceSize(width, height); 
>>> >> mPaint.setColor(ZOOM_BUTTONS_COLOR); 
>>> >> //mySurface.setOnFrameAvailableListener(frameready); 
>>> >> mChart.Redraw(true); 
>>> >> } 
>>> >> @Override 
>>> >> public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) { 
>>> >> // TODO Auto-generated method stub 
>>> >> isRunning = false; 
>>> >> return false; 
>>> >> } 
>>> >> this block below also workswhen manipluating the view later on (pan 
>>> and 
>>> >> zoom) 
>>> >>   public void Render(Bitmap buffmap){ 
>>> >> //mCanvas = null; 
>>> >>  post(new Runnable() { 
>>> >> public void run() { 
>>> >> invalidate(); 
>>> >> mySurface.updateTexImage(); 
>>> >> } 
>>> >> }); 
>>> >> 
>>> >> 
>>> >> The drawing from the worker thread/class is 
>>> >> protected void RenderCanvas(){ 
>>> >> //mCanvas = null; 
>>> >> Canvas c = null; 
>>> >> //synchronized (mCanvas) { 
>>> >> //mCanvas = null; 
>>> >> try { 
>>> >> c = mChartView.lockCanvas(null); 
>>> >> 
>>> >> synchronized (mCanvas) { 
>>> >> c.drawBitmap(buffBitmap, 0, 0, null); 
>>> >> 
>>> >> } 
>>> >> } finally { 
>>> >> if (c != null) { 
>>> >> mChartView.unlockCanvasAndPost(c); 
>>> >> } 
>>> >> } 
>>> >> mChartView.Render(null); 
>>> >> } 
>>> >> 
>>> >> Can I work like this? Non-GL content in a TextureView? 
>>> >> Please help desperate for an answer. 
>>> >> 
>>> >> -- 
>>> >> You received this message because you are subscribed to the Google 
>>> >> Groups "Android Developers" group. 
>>> >> To post to this group, send email to android-d...@googlegroups.com 
>>> >> To unsubscribe from this group, send email to 
>>> >> android-developers+unsubscr...@googlegroups.com 
>>> >> For more options, visit this group at 
>>> >> http://groups.google.com/group/android-developers?hl=en 
>>> > 
>>> > 
>>> > 
>>> > -- 
>>> > Romain Guy 
>>> > Android framework engineer 
>>> > roma...@android.com 
>>>
>>>
>>>
>>> -- 
>>> Romain Guy 
>>> Android framework engineer 
>>> roma...@android.com 
>>>
>>

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to android-developers+unsubscr...@googlegroups.com.
To post to this group, send email to android-developers@googlegroups.com.
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/534e8399-3c3e-48ef-86b0-2e95831596fd%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to