rendaring issue with surfaceview in viewflipper? - android

i am drawing a piechart by extending surfaceview to PieChart Class. now i am creating 2 objects for Piechart and adding to a VieWFlipper to swipe between those charts. now my problem is 2nd Piechart is not visible to the user if user swipes to 2nd view. but all the 2nd pies functionality is working. i am thinking like its refresh problem of the surfaceview.
any help on this will be appreciable. the following is my PieChart class.
class MyPieChart extends SurfaceView implements SurfaceHolder.Callback {
#Override
public void onDraw(Canvas canvas) {
if (hasData) {
resetColor();
try {
canvas.drawColor(getResources().getColor(R.color.graphbg_color));
graphDraw(canvas);
} catch (ValicException ex) {
}
}
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.i("PieChart", "surfaceChanged");
}
public int callCount = 0;
#Override
public void surfaceCreated(SurfaceHolder holder) {
if ((callCount++) % 2 == 0) {
callCount = 1;
try {
Log.i("PieChart", "surfaceCreated");
mChartThread = new ChartThread(getHolder(), this);
mChartThread.setRunning(true);
if (!mChartThread.isAlive()) {
mChartThread.start();
}
mFrame = holder.getSurfaceFrame();
mOvalF = new RectF(0, 0, mFrame.right, mFrame.right);
} catch (Exception e) {
// No error message required
}
}
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.i("PieChart", "surfaceDestroyed");
boolean retry = true;
callCount = 0;
mChartThread.setRunning(false);
while (retry) {
try {
mChartThread.join();
retry = false;
} catch (InterruptedException e) {
// No error message required
}
}
}
}
class ChartThread extends Thread {
private SurfaceHolder mSurfaceHolder;
private PieChart mPieChart;
private boolean mRefresh = false;
public ChartThread(SurfaceHolder surfaceHolder, PieChart pieChart) {
// Log.i("ChartThread", "ChartThread");
mSurfaceHolder = surfaceHolder;
mPieChart = pieChart;
}
public void setRunning(boolean Refresh) {
// Log.i("ChartThread", "setRunning : " + Refresh);
mRefresh = Refresh;
}
#Override
public void run() {
Canvas c;
// Log.i("ChartThread", "run : " + mRefresh);
while (mRefresh) {
c = null;
try {
c = mSurfaceHolder.lockCanvas(mPieChart.mFrame);
// c.drawColor(0xFFebf3f5);
synchronized (mSurfaceHolder) {
mPieChart.onDraw(c);
}
} catch (Exception ex) {
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
mSurfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
here is my flipper.xml
<ViewFlipper
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout android:id="#+id/pie1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout android:id="#+id/pie2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
and i here is my activity
public class ViewFlipperActivity extends Activity {
Button b1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.submit1);
b1.setOnClickListener(new View.OnClickListener() {
ViewFlipper vflipper=(ViewFlipper)findViewById(R.id.flipper);
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
vflipper.showNext();
}
});
}
and i am dding the piecharts to Pie1 and Pie2 LinearLayouts in the Flipper.
both the pie's are created and pasted on to the Pie Layouts. now if i move to the 2nd view in the flipper Pie1 is showing instead of pie2 and all other data and functionality which i am getting is related to Pie2. my doubt is Pie2 is rendering and hidden under Pie1. can any one help me on this with some solution.
i got a break through for this issue. which caused another issue with the following changes.
in flipper.xml replaced LinearLayout i place of view flipper.
<LinearLayout
android:id="#+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout android:id="#+id/pie1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout >
and in ViewFlipperActivity
public class ViewFlipperActivity extends Activity {
Button b1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1 = (Button)findViewById(R.id.submit1);
b1.setOnClickListener(new View.OnClickListener() {
LinearLayout vflipper=(LinearLayout)findViewById(R.id.flipper);
LinearLayout pie1=(LinearLayout)findViewById(R.id.pie1);
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
vflipper.setAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
pie1.removeAllViews();
pie1.addView(PieChart/SurfaceView);
}
});
}
with the animation and is working fine and piechart is getting changed on the view. but block rect is getting visible from the surfaceview for a sec when swiping between piecharts. can some one help me on this issue.

SurfaceView is not going to work properly inside a ViewFlipper as it cannot be animated correctly. This is why Android 4.0 introduces a new widget called TextureView that solves this problem.

here main problem is SurfaceView cannot be animated. that's why neither ViewFlipper nor Layout Animation cannot be applied for SurfaceView.

Related

How to use onDraw/drawRect to make a rectangle based on editText from the mainActivity

Basically I am trying to gather the size of room from the user..(example 20x20 size room). Using those dimensions, collected on the mainActivity, send that data to the Result Activity and use the onDraw/onMeasure to create a simple box.
In short, I want to collect the size of the box in mainActivity from the user. Then in Result activity show the completed image of the box.
I have completed the passing Intents with data from one activity to another. Its using that collected data to draw a shape I am having trouble with.
public class MainActivity extends AppCompatActivity {
//variables
Button btnCalculate, btnReset;
Spinner spinSpeaker;
EditText editLength, editWidth, editHeight;
String t1,t2,t3;
int numLength, numWidth, numHeight, numResult=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//connecting variables to GUI elements
btnCalculate= findViewById(R.id.btnCalculate);
btnReset = findViewById(R.id.btnReset2);
spinSpeaker = findViewById(R.id.spinSpeaker);
editLength= findViewById(R.id.editLength);
editWidth = findViewById(R.id.editWidth);
editHeight = findViewById(R.id.editHeight);
//adding function to the calculate button
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//parsing text to integers
t1=editLength.getText().toString();
t2=editWidth.getText().toString();
t3=editHeight.getText().toString();
if (t3.equals("")) {
editHeight.setText("11");
}else if (t2.equals("")) {
editWidth.setText("11");
}else if (t1.equals("")) {
editLength.setText("11");
}else{
numLength = Integer.parseInt(t1);
numWidth = Integer.parseInt(t2);
numHeight = Integer.parseInt(t3);
}
//alert to contact Pro design
if ((numLength>=999)||(numWidth>=999)||(numHeight>=20)) {
Toast.makeText(MainActivity.this, R.string.contactPro,
Toast.LENGTH_LONG).show();
}else {
//calculating volume
numResult = numHeight * numWidth * numLength;
//sending data to other activities
Intent intent = new Intent(MainActivity.this, Result.class);
intent.putExtra("varHeight", numHeight);
intent.putExtra("varWidth", numWidth);
intent.putExtra("varLength", numLength);
intent.putExtra("varResult", numResult);
startActivity(intent);
}
}
});
}
//Result activity
public class Result extends AppCompatActivity {
TextView tim;
MyCanvas mycanvas;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mycanvas = new MyCanvas(this);
setContentView(R.layout.activity_result);
setContentView(Jim);
//Pulling variables from main activity
Intent variables = getIntent();
//int length = variables.getIntExtra("varLength",0);
//int width =variables.getIntExtra("varWidth",0);
int totalResult = variables.getIntExtra("varResult",0);
//showing total
String result = String.valueOf(totalResult);
tim = findViewById(R.id.txtResult);
tim.setText(result);
//Calling the reset button method
configureResetBtn();
}
public void configureResetBtn(){
Button btnReset=findViewById(R.id.btnReset2);
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//startActivity(new Intent(Result.this, MainActivity.class));
finish();
}
});
}
}
//MyCanvas activity
public class MyCanvas extends AppCompatActivity {
public MyCanvas(Result result) {
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent variables = getIntent();
final int length = variables.getIntExtra("varLength", 0);
final int width = variables.getIntExtra("varWidth", 0);
class Jim extends View {
Paint paint = new Paint();
Rect rect = new Rect();
public Jim(Context context) {
super(context);
}
#Override
protected void onDraw(android.graphics.Canvas canvas) {
super.onDraw(canvas);
paint.setStrokeWidth(3);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(length,width);
}
}
}
}
The expected result is to show a box that is the dimensions based on the user (example 20x20). However I am getting this error:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
But my button works when I remove code trying to draw the box.
Instead of setting content view twice
In ResultActivity
use SurfaceView in R.layout.activity_result
<FrameLayout 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" >
<SurfaceView
android:id="#+id/surface_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
//....
</FrameLayout>
In ResultActivity
SurfaceView surfaceView = findViewById(R.id.surface_view);
surfaceView.getHolder().addCallback(new Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas = holder.lockCanvas();
// draw
Paint paint = new Paint();
paint.setStrokeWidth(3);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);
holder.unlockCanvasAndPost(canvas);
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
});

Adapt camera view with Surfaceview dimension

I'm developing an application where I read some barcode. In a first step I had a big SurfaceView where I can see well the camera preview, but now I would set the dimensions of Surfaceview like the dimensions of barcode but I have bad camera visualization (it is too small). Can someone help me to stretch camera preview? Thanks
Here manage detector and surfaceview:
public class LettoreBarcode extends Fragment {
View view;
SurfaceView surfaceView;
Handler handler;
private BarcodeDetector detector;
private CameraSource cameraSource;
private TextView code;
SparseArray<Barcode> items;
private static Button btnBack;
String barcode = "" ;
SparseArray<Articoli> groups = new SparseArray<Articoli>();
Context _context = null;
ProductsAdapter.ViewHolder _ViewHolder = null;
public LettoreBarcode(){
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_barcode_scanner, container, false);
surfaceView = (SurfaceView) view.findViewById(R.id.surfaceView);
detector = new BarcodeDetector.Builder(getActivity()).setBarcodeFormats(Barcode.ALL_FORMATS).build();
final Dialog d = new Dialog(getActivity());
btnBack = (Button) view.findViewById(R.id.btnBack);
handler = new Handler();
if(!detector.isOperational()){
Toast.makeText(getActivity(), "Detector non attivabile", Toast.LENGTH_LONG).show();
}
cameraSource = new CameraSource.Builder(getActivity(), detector).setAutoFocusEnabled(true).build();
surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
AttivaCamera();
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
});
detector.setProcessor(new Detector.Processor<Barcode>() {
#Override
public void release() {
}
#Override
public void receiveDetections(Detector.Detections<Barcode> detections) {
items = detections.getDetectedItems();
if (items.size() > 0){
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
if (items.valueAt(0) != null){
//do something
handler.postDelayed(new Runnable() {
#Override
public void run() {
DisattivaCamera();
}
},10); //1000
}else
{
d.setContentView(R.layout.dialog_barcode_assente);
d.setTitle("Scanner");
d.setCancelable(true);
d.show();
DisattivaCamera();
}
}
});
}
}
});
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getActivity().onBackPressed();
}
});
return view;
}
private void AttivaCamera()
{
try{
cameraSource.start(surfaceView.getHolder());
}catch(IOException e){
Toast.makeText(getActivity(), "Errore nell'avvio della fotocamera", Toast.LENGTH_LONG).show();
}
}
private void DisattivaCamera()
{
cameraSource.stop();
}
}
It is how I visualize camera with small surfaceview:
https://i.stack.imgur.com/TMunJ.png
I'm new in android development so I'm sorry if could be a lot of mistake in the code.
Sorry for my english also..
Thanks you guys!
In order to display only part of the camera input, i.e. to crop it on the screen, you need a surface view that has dimensions that fit the camera frame aspect ratio, and overlay it with some nontransparent views to leave only part of it visible. Don't put the SurfaeView inside scrolling layout:
So, instead of
<SurfaceView width: match_parent height: 400dp />
you need e.g. FrameLayout as explained here: Is it possible to crop camera preview?
This will not change the frame that arrives to the barcode detector. But this should not worry you; the detector will handle the uncropped image correctly.

.Override method of different class

Calling onBackPressed method of Activity class on a View Class? is it possible? and so how can I? thanks for help
UPDATE: I just created the GameView of my Game which is extends to View Class. I create a variable that increments whenever I finish every level so it will limit a levels per game. And so I want to implement a onBackPressed method which I can set the incrementing variable back to zero whenever the player press the back key.
FULL CODE: MAIN ACTIVITY
public class MainActivity extends Activity {
private GameView mGameView;
#Override
public void onBackPressed() {
if (mGameView.interceptBackPressed()) {
return;
}
// TODO Auto-generated method stub
super.onBackPressed();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
mGameView = (GameView) findViewById(GameView.countmaze);
Afterwards it goes to Menu Class
public class menu extends Activity implements OnClickListener {
static int nextmaze;
int countmaze = 0;
GameView gameView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnplay = (Button) findViewById(R.id.btnPlay);
btnplay.setOnClickListener(this);
}
#Override
public void onBackPressed() {
//I have here an AlertDialog builder
}
#Override
public void onClick(View v) {
case R.id.btnPlay:
Toast.makeText(getApplicationContext(), "LEVEL 1",
Toast.LENGTH_SHORT).show();
startnextmaze();
}
}
void startnextmaze() {
Random rand = new Random();
Intent game = new Intent(menu.this, Game.class);
nextmaze = rand.nextInt(5) + 1;
Maze maze = MazeCreator.getMaze(nextmaze);
game.putExtra("maze", maze);
startActivity(game);
}
Then on my GameView Class
public class GameView extends View {
static int countmaze;
//Big codes here......
public boolean interceptBackPressed() {
// TODO Auto-generated method stub
countmaze = 0;
return true;
}
}
Create a callback on the Activity onBackPressed that the View class will implement
Example
public void interface onBackPressedHandler {
public void onBackPressed();
}
Activity Class
public onBackPressedHandler mHandler;
#Override
public void onCreate(....) {
GameView game... //Inflate or create.
game.setActivity(this);
}
public void setListener(onBackPressedHandler handler) {
mHandler = handler;
}
#Override
public voind onBackPressed() {
if(mHandler != null) {
mHandler.onBackPressed();
}
super.onBackPressed();
View Class
public GameView extends View implements onBackPressedHandler {
public void setActivity(Activity activity) {
activity.setListener(this);
}
public void onBackPressed() {
//your code here.
}
}
This example shows when you press the back Button it will call the onBackPress on the View.
Hope it Helps.
Update your code with the following changes:
Activity
public class MyActivity extends Activity {
private GameView mGameView;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main);
//View reference to object defined in XML
mGameView = (GameView) findViewById(R.id.gameview);//id must be specified in XML
//I assumed that GameView is a part of your XML layout
//View created programmatically.
mGameView = new GameView(this);
//Use one of the above initialisation techniques of mGameView.
}
#Override
public void onBackPressed() {
//here we are calling method on mGameView which cannot be null
//that's why we initialised it in onCreate() method
if (mGameView.interceptBackPressed()) {
return;
}
super.onBackPressed();//finish your Activity
}
}
GameView
public class GameView extends View {
public boolean interceptBackPressed() {
//TODO handle your game logic here and return true if you don't
//want your game to be shutdown, otherwise return false
return true;
}
}

Add AnimationDrawable (PNG Sequence) to Canvas?

I have started venturing into a bit of Canvas stuff and managed to add some statis PNG files to it.
Now I'd like to load in a PNG sequence, which I believe is an AnimationDrawable (rather then Bitmap)
I have written the XML file for the animation but then I am stumped.
I cannot find any examples of people adding PNG sequences to a Canvas object.
here is sample:
public class MainActivity extends Activity {
private static final int FRAME_DELAY = 200; // in ms
private ArrayList<Bitmap> mBitmaps;
private final AtomicInteger mBitmapIndex = new AtomicInteger();
private View mView;
private Thread mThread;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// load resources
mBitmaps = new ArrayList<Bitmap>();
for(int resId : new int[]{
// resource ids here
R.drawable.ic_launcher,
R.drawable.ddms_128,
R.drawable.ddms_icon
}){
mBitmaps.add(BitmapFactory.decodeResource(getResources(), resId));
}
// create View and implement 'draw'
ViewGroup root = (ViewGroup) findViewById(R.id.root);
root.addView(mView = new View(this){
#Override
public void draw(Canvas canvas) {
canvas.drawBitmap(mBitmaps.get(Math.abs(mBitmapIndex.get() % mBitmaps.size())), 10, 10, null);
super.draw(canvas);
}
});
}
#Override
protected void onStart() {
super.onStart();
mThread = new Thread(){
#Override
public void run() {
// wait and invalidate view until interrupted
while(true){
try {
Thread.sleep(FRAME_DELAY);
} catch (InterruptedException e) {
e.printStackTrace();
break; // get out if interrupted
}
mBitmapIndex.incrementAndGet();
mView.postInvalidate();
}
}
};
mThread.start();
}
#Override
protected void onStop() {
mThread.interrupt();
super.onStop();
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
Canvas allows to draw line/shapes/bitmaps.
You managed to draw one bitmap.
Animation is just drawing one bitmap from bunch of them.
Canvas does nothing with drawables.
The logic neither related to Canvas nor to drawables.
Draw the first frame, then use a Handler to post delayed Message that when handled moves to the next frame and calls invalidate().
What you need to do is:
Step #1. Override VisualizerView.verifyDrawable(Drawable who)
#Override
protected boolean verifyDrawable(Drawable who) {
return true;
}
Step #2. Modify MainActivity.addAnimationRenderer()
private void addAnimationRenderer() {
final AnimationDrawable anim = (AnimationDrawable) getResources().getDrawable(R.drawable.png1);
AnimationRenderer animRenderer = new AnimationRenderer(anim);
mVisualizerView.addRenderer(animRenderer);
anim.setCallback(mVisualizerView);
mVisualizerView.post(new Runnable() {
#Override
public void run() {
anim.start();
}
});
}
Step #3. Modify AnimationRenderer: delete
mBitmap.setCallback(null);
mBitmap.start();

TextureView in a Fragment never calls onSurfaceTextureAvailable

I've seen lots of examples using TextureView in a main Activity but I'm trying to put it into a Fragment.
I've created the simplest example possible and its onCreateView is being called, onActivityCreated as well but onSurfaceTextureAvailable isn't being called after passing back the TextureView.
What am I missing ?
Thanks
G
public class FullscreenActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen);
}
}
public class TextureViewFragment extends Fragment
implements TextureView.SurfaceTextureListener {
private TextureView mTextureView;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mTextureView = new TextureView(getActivity());
mTextureView.setSurfaceTextureListener(this);
mTextureView.setOpaque(false);
return mTextureView;
}
#Override
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
}
#Override
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width,
int height) {
// TODO Auto-generated method stub
}
#Override
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
// TODO Auto-generated method stub
}
}
activity_fullscreen.xml:
<FrameLayout 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:background="#0099cc"
tools:context=".FullscreenActivity" >
<fragment class="com.example.test.TextureViewFragment"
android:id="#+id/graphTextureView"
android:layout_width="0px" android:layout_height="match_parent" />
</FrameLayout>
This is because the TextureView must have a nonzero size and be visible. Based on the comment, the layout_width was set to 0px. Change that to a nonzero value.
(Old question but posting answer for posterity)
You should add 'android:hardwareAccelerated=true' to your AndroidManifest.xml.
TextureView needs hardware acceleration.
Your TextureView should be in a layout xml file. Instead of using new to instantiate it you should get it using something like this:
View view = inflater.inflate(R.layout.layout_video, container, false);
mTextureView = (TextureView) view.findViewById(R.id.video_texture_view);
Then in your layout_video.xml file you need a TextureView with id video_texture_view

Categories

Resources