Android - SurfaceView flickers and sometimes does not apply the proper paint - Android Q&A, Help & Troubleshooting

I have been experiencing this issue for quite a while and even after googling a while, I havent found a reasonable solution for my problem.
The issue I am experiencing is that the SurfaceView I am using flickers. Once in a while, the objects I draw flicker and they change their size or colour.
It looks like that sometimes the application skips my calls to 'Paint.setColor()' and the one to 'Paint.setStrokeWidth()'.
I have made methods which change the used paint and then, in order to try to fix this issue, to set it back to the default paint values. Still the issue persists. I have also read that the problem might be due to the double buffering. Is it the case? I am using this code for the DrawingThread:
PS. u can notice that I also tried to use a dirty Rectangle to try to see if the issue can be fixed, but still nothing. [I might not have understood what it actually does.
Code:
class DrawingThread extends Thread {
private SurfaceHolder _surfaceHolder;
private CustomView _cv;
private boolean _run = false;
public DrawingThread(SurfaceHolder surfaceHolder, CustomView cv) {
super();
_surfaceHolder = surfaceHolder;
_cv = cv;
}
public void setRunning(boolean run) {
_run = run;
}
public boolean isRunning() {
return _run;
}
public SurfaceHolder getSurfaceHolder() {
return _surfaceHolder;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
//c = _surfaceHolder.lockCanvas(new Rect(lon - range, lon + range, lat - range, lat + range));
c = _surfaceHolder.lockCanvas();
synchronized (_surfaceHolder) {
_cv.onDraw(c);
}
} finally {
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
PS. I have read somewhere I should draw on a single bitmap and then draw the bitmap on the canvas. I have tried several times but I cannot manage to do so.
Thanks in advance,
N.

Is there anyone who can help me?

Could someone give me a hand?
I cannot find the reason why I cannot fix this issue.
I have tried to draw the items on a bitmap first and then adding it to the canvas, but I didnt manage and I either get a white canvas or a completely black one.
I believe the issue is quite easy to solve after you do it once.

Not even someone who could give some suggestions?

Hey!
As far as I can see the code you posted looks fine. Can you please post the code of your CustomView class? I think there could be an issue there.
Are you trying to make a game?

Hello!!!!
Finally someone
Here is the code I use for the onDraw(canvas):
Code:
@Override
public void onDraw(Canvas canvas) {
try {
canvas.drawColor(Color.WHITE);
pt.setAntiAlias(true);
canvas.rotate(-90, 0, 0);
canvas.translate(translateX, translateY);
canvas.scale(scale, scale);
canvas.rotate(-rotation, lat, lon);
// Drawing Points
Renderer.drawStreet(gaia.getStreets(), scale, canvas, pt);
Renderer.drawPolygon(gaia.getPolygons(), canvas, pt);
Renderer.drawPOI(myContext, gaia.getPOIs(), canvas, pt, scale, rotation);
Renderer.drawCustomPOI(myContext, gaia.getCustomPOI(), canvas, pt, scale, rotation);
if (showLocation) {
drawMarker(canvas);
}
if(drivingMode)
{
Renderer.drawRoute(gaia.getRoute(), canvas, pt);
}
} catch (Exception e) {
e.printStackTrace();
}
}
This is actually a map renderer.
I know the issue is very likely to be due to the double buffering, but I cannot find a way to fix it.
Thanks!
N.

Can you please post the code for the callback methods (onSurfaceCreated, onSurfaceChanged, onSurfaceDestroyed) and maybe the constructor for the view too? I think the problem could be due to the setting up and not the actual rendering.
How are you accessing the surfaceHolder. Are you using the getHolder() method for that? And are you sending the reference you get to the surfaceHolder from getHolder() to the DrawingThread?

Here is the light version of the code:
Code:
public class CustomMapView extends SurfaceView implements
SurfaceHolder.Callback, LocationListener {
private DrawingThread _thread;
private RenderingThread _threadStreets;
Context myContext;
Paint pt = new Paint();
final int SCREEN_HEIGHT = 600;
final int SCREEN_WIDTH = 480;
float scale;
int range = 100000;
static public int rotation = 0;
int sensibility = 100;
int lat;
int lon;
float translateX = -lat * scale - SCREEN_HEIGHT / 2;
float translateY = -lon * scale + SCREEN_WIDTH / 2;
boolean followLocation = true;
boolean isZooming = false;
boolean showLocation = true;
boolean followRotation = false;
static boolean drivingMode = false;
boolean DEBUG = true;
Handler mHandler;
public static GaiaApp gaia;
public CustomMapView(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
gaia = ((GaiaApp) context.getApplicationContext());
scale = gaia.getCurScale();
lat = (int) gaia.getCurPosition().getLatitude();
lon = (int) gaia.getCurPosition().getLongitude();
pf = new PathFinder();
startupDB();
myContext = context;
mHandler = new Handler();
_thread = new DrawingThread(getHolder(), this);
_threadStreets = new RenderingThread(this);
setFocusable(true);
pt.setFlags(Paint.DITHER_FLAG);
pt.setFilterBitmap(true);
this.invalidate();
}
@Override
public void onDraw(Canvas canvas) {
try {
canvas.drawColor(Color.WHITE);
pt.setAntiAlias(true);
canvas.rotate(-90, 0, 0);
canvas.translate(translateX, translateY);
canvas.scale(scale, scale);
canvas.rotate(-rotation, lat, lon);
// Drawing Points
Renderer.drawStreet(gaia.getStreets(), scale, canvas, pt);
Renderer.drawPolygon(gaia.getPolygons(), canvas, pt);
Renderer.drawPOI(myContext, gaia.getPOIs(), canvas, pt, scale, rotation);
Renderer.drawCustomPOI(myContext, gaia.getCustomPOI(), canvas, pt, scale, rotation);
if (showLocation) {
drawMarker(canvas);
}
if(drivingMode)
{
Renderer.drawRoute(gaia.getRoute(), canvas, pt);
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
if (!_thread.isRunning()) {
_thread.setRunning(true);
_thread.start();
}
if (!_threadStreets.isRunning()) {
_threadStreets.setRunning(true);
_threadStreets.start();
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
boolean retry = true;
_thread.setRunning(false);
_threadStreets.setRunning(false);
while (retry) {
try {
_thread.join();
_threadStreets.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
class DrawingThread extends Thread {
private SurfaceHolder _surfaceHolder;
private CustomMapView _cmv;
private boolean _run = false;
public DrawingThread(SurfaceHolder surfaceHolder, CustomMapView cmv) {
super();
_surfaceHolder = surfaceHolder;
_cmv = cmv;
}
public void setRunning(boolean run) {
_run = run;
}
public boolean isRunning() {
return _run;
}
public SurfaceHolder getSurfaceHolder() {
return _surfaceHolder;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas();
synchronized (_surfaceHolder) {
_cmv.onDraw(c);
}
} finally {
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
class RenderingThread extends Thread {
CustomMapView _cmv;
Boolean _run = false;
public RenderingThread(CustomMapView cmv) {
super();
_cmv = cmv;
}
public void setRunning(boolean run) {
_run = run;
}
public boolean isRunning() {
return _run;
}
@Override
public void run() {
while (_run) {
try {
Log.d("CustomMapView", "Retrieving...");
new StreetRunnable(_cmv).run();
Log.d("CustomMapView", "Retrieving");
if(!followRotation)
rotation = 0;
else
rotation = gaia.getOrientation();
Log.d("CustomMapView", "Updating...");
mHandler.post(new Runnable() {
@Override
public void run() {
_cmv.invalidate();
}
});
Log.d("CustomMapView", "Updated");
} finally {
}
}
}
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}

Hello,
have you had the chance to check it out?
Thanks
N.

Sorry about that. I did not have access to the internet for the last 2 - 3 days.
Anyways I went through your code...If that is what you call light I wouldn't want to know whats heavy
I noticed that you have two threads which are responsible for drawing to the same view. You are using the DrawingThread to call the onDraw method of your CustomMapView object and the RenderingThread to post the invalidate method of the same CustomMapView object, which also results in a call to the onDraw method of the same view.
I am not really sure if doing this is a good thing and maybe this is what is causing the problems with the rendering? As a test, you could comment out one of the Thread.start() calls to one of the two rendering threads in your onSurfaceCreated method and check if it stops flickering.
I know this isn't how you want your app to function but if it fixes the flickering you'd know its the the two drawing threads that are causing the problem.
Code:
@Override
public void surfaceCreated(SurfaceHolder holder) {
// Comment out either one of the following two if blocks
if (!_thread.isRunning()) {
_thread.setRunning(true);
_thread.start();
}
if (!_threadStreets.isRunning()) {
_threadStreets.setRunning(true);
_threadStreets.start();
}
}

Awesome dude!
It fixed it straight away.
The reason is that I got a thread first and then added a new one, without thinking about just merging them both together
Thanks again!

Yep. You're welcome!
You'll just have to redesign your code to use just one rendering thread and it should work fine. All the best!

Related

Using multiple drawables with MapsForge

After following the developer tutorial using Google Maps, my code is crashing when trying to execute the map code from the start up screen. I am trying to add Big East basketball team icons on the map in the correct GPS location. When that part of the code is taken out it works but I can't find what I'm doing wrong. Thanks for any help you can provide.
Here is the map code:
Code:
public class MapsForgeViewer extends MapActivity implements LocationListener, OnClickListener {
private static final double latitudePitt = 40.443061;
private static final double longitudePitt = -79.962273;
private static final double latitudeUConn = 41.807975;
private static final double longitudeUConn = -72.253626;
private MapView mapView;
private GeoPoint myCurrentLocation;
private Button findPosition;
private Button roster;
private Button schedule;
private Button stats;
private Button exit;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.map_view);
// setting up the location listener
LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
100, 0, mlocListener);
// end of location listener setup
// find from XML and set onClickListeners
findPosition = (Button) findViewById(R.id.findPositionButton);
roster = (Button) findViewById(R.id.roster);
schedule = (Button) findViewById(R.id.schedule);
stats = (Button) findViewById(R.id.stats);
exit = (Button) findViewById(R.id.exit);
findPosition.setOnClickListener(this);
roster.setOnClickListener(this);
schedule.setOnClickListener(this);
stats.setOnClickListener(this);
exit.setOnClickListener(this);
// end of mapView layout setup
// setting the up the map at the proper zoom level and creating the
// scale bars and buttons
mapView = (MapView) findViewById(R.id.mapView);
mapView.setMapViewMode(MapViewMode.MAPNIK_TILE_DOWNLOAD);
mapView.setBuiltInZoomControls(true);
mapView.setScaleBar(true);
mapView.setClickable(true);
findPositionButton(myCurrentLocation);
mapView.getController().setZoom(14);
setCenterlocation();
// end of mapView setup
//Put pins on map
List<Overlay> mapOverlays = mapView.getOverlays();
Drawable drawablePitt = this.getResources().getDrawable(R.drawable.pittspin);
CustomItemizedOverlay itemizedOverlayPitt = new CustomItemizedOverlay(drawablePitt, this);
Drawable drawableUConn = this.getResources().getDrawable(R.drawable.connpin);
CustomItemizedOverlay itemizedOverlayUConn = new CustomItemizedOverlay(drawableUConn, this);
//Create points using latitude and longitude
GeoPoint pointPitt = new GeoPoint(latitudePitt, longitudePitt);
OverlayItem overlayitemPitt = new OverlayItem(pointPitt, "Pitt", "Panthers");
GeoPoint pointUConn = new GeoPoint(latitudeUConn, longitudeUConn);
OverlayItem overlayitemUConn = new OverlayItem(pointUConn, "UConn", "Huskies");
itemizedOverlayPitt.addOverlay(overlayitemPitt);
itemizedOverlayUConn.addOverlay(overlayitemUConn);
//add to map
mapOverlays.add(itemizedOverlayPitt);
mapOverlays.add(itemizedOverlayUConn);
}
public void onClick(View v)
{
switch (v.getId())
{
case (R.id.findPositionButton):
findPositionButton(myCurrentLocation);
break;
case (R.id.roster):
// get data from database
break;
case (R.id.schedule):
break;
case (R.id.stats):
break;
// close the app
case (R.id.exit):
finish();
break;
}
}
public void findPositionButton(GeoPoint p)
{
mapView.getController().setCenter(p);
}
@Override
// resumes the actions of the application on a pause
protected void onResume()
{
super.onResume();
}
// sets the center of the screen on the map
protected void setCenterlocation()
{
if (myCurrentLocation == null)
mapView.getController().setCenter(new GeoPoint(39.00, -100.00));
else
mapView.getController().setCenter(myCurrentLocation);
}
public class MyLocationListener implements LocationListener
{
public void onLocationChanged(Location loc)
{
GeoPoint gp = new GeoPoint(loc.getLatitude(), loc.getLongitude());
if (gp != null)
myCurrentLocation = gp;
}
// activates when the current provider is disabled, or inactive
public void onProviderDisabled(String provider)
{
Toast.makeText(getApplicationContext(), "GPS Disabled",
Toast.LENGTH_LONG).show();
}
// activates when a provider is found.
public void onProviderEnabled(String provider)
{
Toast.makeText(getApplicationContext(), "GPS Enabled",
Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
}
public void onLocationChanged(Location arg0)
{
}
public void onProviderDisabled(String provider)
{
}
public void onProviderEnabled(String provider)
{
}
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
CustomItemizedOverlay:
Code:
public class CustomItemizedOverlay extends ItemizedOverlay<OverlayItem> {
public ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context context;
public MapsForgeViewer mActivtyl;
public CustomItemizedOverlay(Drawable defaultMarker) {
super(boundCenterBottom(defaultMarker));
}
public CustomItemizedOverlay(Drawable defaultMarker, Context context) {
this(defaultMarker);
this.context = context;
}
@Override
protected OverlayItem createItem(int i) {
return mapOverlays.get(i);
}
@Override
public int size() {
return mapOverlays.size();
}
@Override
protected boolean onTap(int index) {
OverlayItem item = mapOverlays.get(index);
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle(item.getTitle());
dialog.setMessage(item.getSnippet());
dialog.show();
return true;
}
public void addOverlay(OverlayItem overlay) {
mapOverlays.add(overlay);
this.populate();
}
}

[Q] Android Maps draw path problem

I am developing an application which draws the path of the user as he moves and calculates the area .
This is the code i am trying my problem is at the start of recording the path the path is drawn even if the user has not moved and sometimes when even if user is moving the path is not drawn .
RouteOverlay class:
public class RouteOverlay extends Overlay {
private GeoPoint gp1;
private GeoPoint gp2;
private int mode = 1;
public RouteOverlay(GeoPoint paramGeoPoint1, GeoPoint paramGeoPoint2,int paramInt)
{
this.gp1 = paramGeoPoint1;
this.gp2 = paramGeoPoint2;
this.mode = paramInt;
}
public void draw(Canvas paramCanvas, MapView paramMapView,
boolean paramShadow)
{
super.draw(paramCanvas, paramMapView, paramShadow);
Projection projection = paramMapView.getProjection();
Paint mPaint = new Paint();
mPaint.setDither(true);
mPaint.setAntiAlias(true);
mPaint.setColor(Color.RED);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);
mPaint.setAlpha(120);
Point p1 = new Point();
Point p2 = new Point();
Path path = new Path();
projection.toPixels(gp1, p1);
projection.toPixels(gp2, p2);
path.moveTo(p2.x,p2.y);
path.lineTo(p1.x,p1.y);
paramCanvas.drawPath(path, mPaint);
}
MainActivity:
public class MainActivity extends MapActivity {
public final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
coordinates.add(location);
mapView.getController().animateTo(getGeoByLocation(location));
drawRoute(coordinates, mapView);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_area_measurement);
this.mapView = ((MapView) findViewById(R.id.mapview));
this.mapView.setBuiltInZoomControls(false);
this.mapView.getController().setZoom(17);
this.coordinates = new ArrayList<Location>();
}
protected boolean isRouteDisplayed() {
return false;
}
private GeoPoint getGeoByLocation(Location location) {
GeoPoint gp = null;
try {
if (location != null) {
double geoLatitude = location.getLatitude() * 1E6;
double geoLongitude = location.getLongitude() * 1E6;
gp = new GeoPoint((int) geoLatitude, (int) geoLongitude);
}
} catch (Exception e) {
e.printStackTrace();
}
return gp;
}
public String getLocationProvider(LocationManager paramLocationManager) {
try {
Criteria localCriteria = new Criteria();
localCriteria.setAccuracy(1);
localCriteria.setAltitudeRequired(false);
localCriteria.setBearingRequired(false);
localCriteria.setCostAllowed(true);
localCriteria.setPowerRequirement(3);
String str = paramLocationManager.getBestProvider(localCriteria,
true);
return str;
} catch (Exception localException) {
while (true) {
localException.printStackTrace();
}
}
}
private void drawRoute(ArrayList<Location> paramArrayList,MapView paramMapView) {
List<Overlay> overlays = paramMapView.getOverlays();
//Changed for smooth rendering
overlays.clear();
for (int i = 1; i < paramArrayList.size(); i++) {
overlays.add(new RouteOverlay(getGeoByLocation(paramArrayList.get(i - 1)), getGeoByLocation(paramArrayList.get(i)),2));
}
}
public void startRecording() {
this.isMeasuring = true;
lm = (LocationManager) getSystemService(LOCATION_SERVICE);
lm.requestLocationUpdates(getLocationProvider(lm),500,2,this.locationListener);
/*if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
gpsstatus.setText("Gps Is Enabled");
}else
{ gpsstatus.setText("Gps Is disabled");}*/
}
This probably has to do with the accuracy of the GPS. I didn't really look at your code but maybe you should add some kind of buffer. if (movement>5m){//draw stuff}

[Q] Options menu problem

I'm trying to get a working option menu, and I can get it to work with any activity i got in my app, but not the Main one. that's mean, whenever i try to access the main activity from the option menu, i get an error. here is the main activity, maybe it'll help:
public class Main extends Activity {
private ContentResolver cResolver;
private Window window;
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadPrefsBrightness();
SharedPreferences sp1 = getSharedPreferences("ButtonData", MODE_PRIVATE);
boolean cbValue = sp1.getBoolean("ButtonData", false);
if(cbValue){
Music.play(this, R.raw.japanese_garden);
Music.stoping();
}else{
Music.play(this, R.raw.japanese_garden);
}
SharedPreferences sp3 = getSharedPreferences("SleepData", MODE_PRIVATE);
boolean cbValue2 = sp3.getBoolean("SleepData", false);
if(cbValue2){
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}else{
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
public void loadPrefsBrightness() {
SharedPreferences sp2 = getSharedPreferences("BrightnessData", MODE_PRIVATE);
int bnessValue = sp2.getInt("BrightnessData", 150);
cResolver = getContentResolver();
window = getWindow();
android.provider.Settings.System.putInt(cResolver, android.provider.Settings.System.SCREEN_BRIGHTNESS, bnessValue);
LayoutParams layoutpars = window.getAttributes();
layoutpars.screenBrightness = bnessValue / (float)255;
window.setAttributes(layoutpars);
}
public void setting_onclick(View view) {
Intent i = new Intent("net.lirazarviv.Setting");
startActivity(i);
}
public void free_search_onclick(View view) {
Intent i = new Intent("net.lirazarviv.FreeSearch");
startActivity(i);
}
public void search_by_type_onclick(View view) {
Intent i = new Intent("net.lirazarviv.SearchByType");
startActivity(i);
}
public void search_by_difficulty_onclick(View view) {
Intent i = new Intent("net.lirazarviv.SearchByDifficulty");
startActivity(i);
}
public void info_onclick(View view) {
Intent i = new Intent("net.lirazarviv.Info");
startActivity(i);
}
@override
protected void onPause() {
if (this.isFinishing()){ //basically BACK was pressed from this activity
Music.stop();
}
Context context = getApplicationContext();
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> taskInfo = am.getRunningTasks(1);
if (!taskInfo.isEmpty()) {
ComponentName topActivity = taskInfo.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
Music.stop();
}
}
super.onPause();
}
}
Why not post the logcat of eclipse? It will help to track down the bug easier
Sent from my GT-N7000 using xda app-developers app

[Q] Gallery App from Stock S3 (same as Note 2) to AOSP Roms like CM 10.1

Hello guys first of all I'm new here so i hope i make everything correct and I'm in the right section.
I just searched for the Stock S3 Gallery (the same like Note 2 with Gallery Flow Effect) but I couldn't find somehting that runs on AOSP Roms, so I just googled a little bit and found these Code (Source was stackoverflow.com, sorry but I can't post the links because Forum Rules with 10 Posts).
Code:
gal_caterories.setAdapter(new CategoryPreferenceAdapter (context,
response.customOfferPojo.offerAvailableCategories,1));
public class CategoryPreferenceAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
public static List<String> thumbnailsselection;
private ViewHolder holder = new ViewHolder();
private List<CategoriesClass> categories = new ArrayList<CategoriesClass>();
public static final int ACTIVITY_CREATE = 10;
public static List<Integer> prefferedCategories = new ArrayList<Integer>();
private int i = 0;
// Keep all Images in array
// public Integer[] mThumbIds = { R.drawable.pic_1, R.drawable.pic_2,
// R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
// R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8,
// R.drawable.pic_9, R.drawable.pic_10, R.drawable.pic_11,
// R.drawable.pic_12, R.drawable.pic_13, R.drawable.pic_14,
// R.drawable.pic_15 };
public CategoryPreferenceAdapter(Context c,
List<CategoriesClass> categories, List<Integer> prefferedCategories) {
this.mContext = c;
this.categories = categories;
this.prefferedCategories = prefferedCategories;
mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thumbnailsselection = new ArrayList<String>();
}
class ViewHolder {
ImageView imageview;
TextView tv_categoryname;
// LinearLayout ll_category_selection;
CheckBox cb_check_category;
}
[user=439709]@override[/user]
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = mInflater
.inflate(R.layout.template_categories_grid, null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.iv_image_category);
holder.tv_categoryname = (TextView) convertView
.findViewById(R.id.tv_categoryname);
// holder.ll_category_selection = (LinearLayout) convertView
// .findViewById(R.id.ll_category_selection);
holder.cb_check_category = (CheckBox) convertView
.findViewById(R.id.cb_check_category);
if (prefferedCategories.contains(categories.get(position).categoryid)) {
holder.cb_check_category.setChecked(true);
} else {
holder.cb_check_category.setChecked(false);
holder.cb_check_category.setBackgroundColor(Color.TRANSPARENT);
}
holder.tv_categoryname.setText(categories.get(position).category
.toUpperCase());
holder.cb_check_category
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
prefferedCategories.add(categories.get(position).categoryid);
} else {
holder.cb_check_category
.setBackgroundColor(Color.TRANSPARENT);
prefferedCategories.remove(categories.get(position).categoryid);
}
}
});
ImageLoader imageload = new ImageLoader(mContext);
imageload.DisplayImage(
categories.get(position).defaultCategoryImage.toString(),
holder.imageview);
holder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
return convertView;
}
[user=439709]@override[/user]
public int getCount() {
// TODO Auto-generated method stub
return categories.size();
}
[user=439709]@override[/user]
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
[user=439709]@override[/user]
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
Can someone maybe build an App with this Code? I ask because I have no clue about that, but I think the sourcecode is good and many guys would be happy. Maybe some Guy has the time to do it? (It is maybe a cool App for the Playstore too).
Thank you for the attention.
Lostion said:
Hello guys first of all I'm new here so i hope i make everything correct and I'm in the right section.
I just searched for the Stock S3 Gallery (the same like Note 2 with Gallery Flow Effect) but I couldn't find somehting that runs on AOSP Roms, so I just googled a little bit and found these Code (Source was stackoverflow.com, sorry but I can't post the links because Forum Rules with 10 Posts).
Code:
gal_caterories.setAdapter(new CategoryPreferenceAdapter (context,
response.customOfferPojo.offerAvailableCategories,1));
public class CategoryPreferenceAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
public static List<String> thumbnailsselection;
private ViewHolder holder = new ViewHolder();
private List<CategoriesClass> categories = new ArrayList<CategoriesClass>();
public static final int ACTIVITY_CREATE = 10;
public static List<Integer> prefferedCategories = new ArrayList<Integer>();
private int i = 0;
// Keep all Images in array
// public Integer[] mThumbIds = { R.drawable.pic_1, R.drawable.pic_2,
// R.drawable.pic_3, R.drawable.pic_4, R.drawable.pic_5,
// R.drawable.pic_6, R.drawable.pic_7, R.drawable.pic_8,
// R.drawable.pic_9, R.drawable.pic_10, R.drawable.pic_11,
// R.drawable.pic_12, R.drawable.pic_13, R.drawable.pic_14,
// R.drawable.pic_15 };
public CategoryPreferenceAdapter(Context c,
List<CategoriesClass> categories, List<Integer> prefferedCategories) {
this.mContext = c;
this.categories = categories;
this.prefferedCategories = prefferedCategories;
mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
thumbnailsselection = new ArrayList<String>();
}
class ViewHolder {
ImageView imageview;
TextView tv_categoryname;
// LinearLayout ll_category_selection;
CheckBox cb_check_category;
}
[user=439709]@override[/user]
public View getView(final int position, View convertView, ViewGroup parent) {
convertView = mInflater
.inflate(R.layout.template_categories_grid, null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.iv_image_category);
holder.tv_categoryname = (TextView) convertView
.findViewById(R.id.tv_categoryname);
// holder.ll_category_selection = (LinearLayout) convertView
// .findViewById(R.id.ll_category_selection);
holder.cb_check_category = (CheckBox) convertView
.findViewById(R.id.cb_check_category);
if (prefferedCategories.contains(categories.get(position).categoryid)) {
holder.cb_check_category.setChecked(true);
} else {
holder.cb_check_category.setChecked(false);
holder.cb_check_category.setBackgroundColor(Color.TRANSPARENT);
}
holder.tv_categoryname.setText(categories.get(position).category
.toUpperCase());
holder.cb_check_category
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked) {
prefferedCategories.add(categories.get(position).categoryid);
} else {
holder.cb_check_category
.setBackgroundColor(Color.TRANSPARENT);
prefferedCategories.remove(categories.get(position).categoryid);
}
}
});
ImageLoader imageload = new ImageLoader(mContext);
imageload.DisplayImage(
categories.get(position).defaultCategoryImage.toString(),
holder.imageview);
holder.imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
return convertView;
}
[user=439709]@override[/user]
public int getCount() {
// TODO Auto-generated method stub
return categories.size();
}
[user=439709]@override[/user]
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
[user=439709]@override[/user]
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
}
Can someone maybe build an App with this Code? I ask because I have no clue about that, but I think the sourcecode is good and many guys would be happy. Maybe some Guy has the time to do it? (It is maybe a cool App for the Playstore too).
Thank you for the attention.
Click to expand...
Click to collapse
Me too .Looking for the same thing for a long time.
But could not find any.:crying:
Just wishing some guy to build an apk that would run on all phones like some other games.
Keep searching and kindly share if you find any.:good:
Thanks
Thank you man for posting this i think a lot of us wish to get this app on cyanogenmod for example i hope there's a hero dev. Can do this for us
Regards
تم الإرسال من جهازي GT-I9300 بواسطة تاباتلك 4 now Free

Camera2 API running in background service

Hello everybody
Unfortunately, I have too few post to make a thread in the " Android Software Development" forum. I would be very happy if this post can be moved to it.
I have used the Camera2 API to implement a background video recorder running as a service and recording from the front cam. For this I have created a new SurfaceView, set its size to 1x1 and moved it to the top left corner. My code is shown below. I'm using Android 5.1.
With the Camera API it worked very well but unfortunately the frame rate is only at 20 fps (and when I increase the exposure compensation it drops even more), although with the Open Camera app I have 30 fps also with increased exposure compensation. That is why I want to try Camera2 API (hoping to get higher fps). Unfortunately, I'm getting the following error:
MediaRecoder: setOutputFile called in an invalid state(2)
MediaRecorder: start called in an invalid state: 2
Click to expand...
Click to collapse
Here is my code:
Code:
public class RecorderServiceCamera2 extends Service implements SurfaceHolder.Callback {
private WindowManager windowManager;
private SurfaceView surfaceView;
private CameraDevice mCamera;
private MediaRecorder mediaRecorder = null;
private CaptureRequest mCaptureRequest;
private CameraCaptureSession mSession;
@Override
public void onCreate() {
// Create new SurfaceView, set its size to 1x1, move it to the top left corner and set this service as a callback
windowManager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
surfaceView = new SurfaceView(this);
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(
1, 1,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT
);
layoutParams.gravity = Gravity.LEFT | Gravity.TOP;
windowManager.addView(surfaceView, layoutParams);
surfaceView.getHolder().addCallback(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Intent notificationIntent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
Notification notification = new NotificationCompat.Builder(this)
//.setSmallIcon(R.mipmap.app_icon)
.setContentTitle("Background Video Recorder")
.setContentText("")
.setContentIntent(pendingIntent).build();
startForeground(MainActivity.NOTIFICATION_ID_RECORDER_SERVICE, notification);
return Service.START_NOT_STICKY;
}
@Override
public void surfaceCreated(final SurfaceHolder surfaceHolder) {
mediaRecorder = new MediaRecorder();
try {
CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);
String[] cameras = manager.getCameraIdList();
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameras[1]);
StreamConfigurationMap configs = characteristics.get(
CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
Size[] sizes = configs.getOutputSizes(MediaCodec.class);
final Size sizeHigh = sizes[0];
manager.openCamera(cameras[1], new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
mCamera = camera;
mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface());
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mediaRecorder.setMaxFileSize(0);
mediaRecorder.setOrientationHint(0);
mediaRecorder.setOutputFile("test.mp4");
try { mediaRecorder.prepare(); } catch (Exception ignored) {}
List<Surface> list = new ArrayList<>();
list.add(surfaceHolder.getSurface());
try {
CaptureRequest.Builder captureRequest = mCamera.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
captureRequest.addTarget(surfaceHolder.getSurface());
mCaptureRequest = captureRequest.build();
} catch (CameraAccessException e) {
e.printStackTrace();
}
try {
mCamera.createCaptureSession(list, new CameraCaptureSession.StateCallback() {
@Override
public void onConfigured(@NonNull CameraCaptureSession session) {
mSession = session;
}
@Override
public void onConfigureFailed(@NonNull CameraCaptureSession session) {
mSession = session;
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
mediaRecorder.start();
try {
mSession.setRepeatingRequest(mCaptureRequest,
new CameraCaptureSession.CaptureCallback() {
@Override
public void onCaptureStarted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, long timestamp, long frameNumber) {
super.onCaptureStarted(session, request, timestamp, frameNumber);
}
@Override
public void onCaptureCompleted(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull TotalCaptureResult result) {
super.onCaptureCompleted(session, request, result);
}
@Override
public void onCaptureFailed(@NonNull CameraCaptureSession session, @NonNull CaptureRequest request, @NonNull CaptureFailure failure) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
}
@Override
public void onError(@NonNull CameraDevice camera, int error) {
}
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
// Stop recording and remove SurfaceView
@Override
public void onDestroy() {
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
mCamera.close();
mediaRecorder= null;
windowManager.removeView(surfaceView);
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {}
@Override
public IBinder onBind(Intent intent) { return null; }
}
Hi, were you able to get this code to work without the errors?

Categories

Resources