SurfaceView implements Runnable Android Code
package cz.okhelp.surfview;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
public class MainActivity extends Activity implements OnTouchListener {
OurView v;
Bitmap ball, blob;
float x, y;
Sprite sprite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_our_view);
v = new OurView(this);
v.setOnTouchListener(this);
ball = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
x = y = 0;
setContentView(v);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
v.pause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable {
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
public OurView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = getHolder();
}
public void run() {
// TODO Auto-generated method stub
while (isItOK == true) {
// perform canvas drawing
if (!holder.getSurface().isValid()) {
continue;
}
// sprite = new Sprite(OurView.this,blob);
Canvas c = holder.lockCanvas();
c.drawARGB(255, 100, 100, 10);
c.drawBitmap(ball, x, y, null);
// onDraw(c);
holder.unlockCanvasAndPost(c);
}
}
protected void onDraw(Canvas canvas) {
// sprite.onDraw(canvas);
}
public void pause() {
isItOK = false;
while (true) {
try {
t.join();
} catch (InterruptedException e) {
// TODO: handle exception
e.printStackTrace();
}
break;
}// end while
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
break;
case MotionEvent.ACTION_UP:
x = event.getX();
y = event.getY();
break;
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
break;
}// end switch
return true;
}
}
396LW NO topic_id
AD
Další témata ....(Topics)
ListFragment show only one item issue
Try to change layout_width to 0dp
Try to change layout_width to 0dp
<fragment android:name="cz.okhelp.android.fragchangfromlist.ArticleFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
<!--todo LAYOUT_WIDTH have to be 0dp !!!!!!!!!!!!!!!!!!!!! -->
<FrameLayout android:id="@+id/article_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2" />
Try this source code:
Issue: Immutable bitmap passed to Canvas constructor
Solution: Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.my_image)
.copy(Bitmap.Config.ARGB_8888, true);
Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.my_image)
.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(myBitmap);
// etc. .......
Issue: Immutable bitmap passed to Canvas constructor
Solution: Bitmap myBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.my_image)
.copy(Bitmap.Config.ARGB_8888, true);
You have to add constructor!
public static class YourFragment extends Fragment {
//you have to add constructor!!!!!
public YourFragment(){}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_main, container, false);
return view;
}
}
File->Settings->Plugins and disable some plugins:
Use Emulator AVD with small memmory usage. For example: Virtual tablet with hight resolution have big memmory usage. Virtual phone with 240x320 resolution have small memmory usage.
Use instaed of Emulator, real device connected by USB (smarphone Samsung Galaxy or other recommended by Google whit debugable mode).
If you notice that Android Studio works slowly, consider the possibility to reduce the number of folders under antivirus protection.
Each antivirus check in your project consumes resources. You can significantly improve the performance, if you exclude certain folders from the antivirus protection.
- Google Cloud Testing
- Google Cloud Tools Core
- Google Cloud Tools for Android Studio
- CVS Integration
- Git Integration
- GitHub
- hg4idea
- Subversion Integration
Use Emulator AVD with small memmory usage. For example: Virtual tablet with hight resolution have big memmory usage. Virtual phone with 240x320 resolution have small memmory usage.
Use instaed of Emulator, real device connected by USB (smarphone Samsung Galaxy or other recommended by Google whit debugable mode).
If you notice that Android Studio works slowly, consider the possibility to reduce the number of folders under antivirus protection.
Each antivirus check in your project consumes resources. You can significantly improve the performance, if you exclude certain folders from the antivirus protection.
// in AndroidManifest.xml
<Activity
android:screenOrientation="portrait" // or landscape
// in MyActivity.java
boolean mbOrientacionLandscape = false;
int nOrientation = getResources().getConfiguration().orientation;
if(mbOrientacionLandscape==true){
getResources().getConfiguration();
if(nOrientation != Configuration.ORIENTATION_LANDSCAPE)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
else{
if(nOrientation != Configuration.ORIENTATION_PORTRAIT)
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
Editace: 2013-12-09 12:58:17
Počet článků v kategorii: 396
Url:surfaceview-implements-runnable-android-code