Using Handler to set Visibile/Invisibile a maker - android

I'm working with the google map for a project. I'm using an handler to set the marker visible o invisibile.
If the zoom of the camera is over the a number I have to set the marker invisible or if the zoom is lower than a number I have to set the marker Invisible. I'm using an ArrayList to save all the markers.
I need to create a timer that will continue to work until the application is closed
My problem is that the handler doesn't start.
Below there is my code:
final Handler handler = new Handler();
Runnable run = new Runnable() {
#Override
public void run() {
if (mMap.getCameraPosition().zoom > zoomLevel) {
for (int j = 0; j < players.size(); j++) {
Marker removeMarker = players.get(j).marker;
removeMarker.setVisible(false);
}
}else {
for (int j = 0; j < players.size(); j++) {
Marker removeMarker = players.get(j).marker;
removeMarker.setVisible(true);
}
}
handler.postDelayed(run, 1000);
}
};

this handler.postDelayed(run, 1000); needs to be outside of the runnable body
final Handler handler = new Handler();
Runnable run = new Runnable() {
#Override
public void run() {
if (mMap.getCameraPosition().zoom > zoomLevel) {
for (int j = 0; j < players.size(); j++) {
Marker removeMarker = players.get(j).marker;
removeMarker.setVisible(false);
}
}else {
for (int j = 0; j < players.size(); j++) {
Marker removeMarker = players.get(j).marker;
removeMarker.setVisible(true);
}
}
}
};
handler.postDelayed(run, 1000);

Related

RecyclerViews and AnimationDrawables - how can I put AnimationDrawables into a ViewHolder without them stopping on having another view in the window?

Basically, I have an AnimationDrawable that I generated with a helper method:
public static AnimationDrawable requestLoadingDrawable(Context originContext) {
if(panoramaLoadingLoopDrawable == null) {
panoramaLoadingLoopDrawable = new AnimationDrawable();
Bitmap src = BitmapFactory.decodeResource(originContext.getResources(), R.drawable.load360_3lines);
BitmapDrawable frameInDrawable;
//int squareInstDim = (int)(200*adapterCtx.getResources().getDisplayMetrics().density);
int horiDim = (int)(src.getWidth() / 10);
int vertDim = (int)(src.getHeight() / 3);
Bitmap frame;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 10; j++) {
frame = Bitmap.createBitmap(src, j * horiDim, i*vertDim, horiDim, vertDim);
frameInDrawable = new BitmapDrawable(originContext.getResources(), frame);
panoramaLoadingLoopDrawable.addFrame(frameInDrawable, (1000/30));
}
}
src.recycle();
}
panoramaLoadingLoopDrawable.setOneShot(false);
return panoramaLoadingLoopDrawable;
}
The panoramaLoadingLoopDrawable is private static in this helper class.
When I put it in the RecyclerView, when there's 2 items that use this AnimationDrawable in the ViewHolder, both would stop animating.
Should I make the panoramaLoadingLoopDrawable non-static or something?

How to faster renew ImageView by Bitmap

I want to renew imageView fastly. My code is that ImageView is changed every 3 colors. It operates correctly. but very slow. I want to faster operate the code. what should i do? This is my code.
public class MainActivity extends AppCompatActivity {
ImageView img;
int[] arr = new int[300];
int colorArr[] = {Color.BLACK, Color.GREEN, Color.RED};
char color = 0;
DisplayMetrics dm;
Bitmap bit;
int x = 1, y = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.image);
bit = Bitmap.createBitmap(300, 400, RGB_565);
for(int i = 0 ; i < 300; i++)
arr[i] = colorArr[color%3];
for(int i = 0; i < 300; i++){
for(int j = 0; j < 400; j++)
bit.setPixel(i,j, Color.WHITE);
}
img.post(new Runnable() {
#Override
public void run() {
bit.setPixels(arr, 0, 300, 0, y++, 300,1);
img.setImageBitmap(bit);
img.post(this);
if(y == 400){
color++;
for(int i = 0; i < 300; i++)
arr[i] = colorArr[color%3];
y = 0;
}
}
});
}
}

android ImageView is not Working

I want to renew ImageView whenever set pixel of Bitmap class. but ImageView is not working. I already use ui thread. what should i do?
this is my code.
public class MainActivity extends AppCompatActivity {
ImageView img;
DisplayMetrics dm;
Bitmap bit;
int x = 1, y = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView)findViewById(R.id.image);
bit = Bitmap.createBitmap(300, 400, RGB_565);
for(int i = 0; i < 300; i++){
for(int j = 0; j < 400; j++)
bit.setPixel(i,j, Color.WHITE);
}
runOnUiThread(new Runnable() {
#Override
public void run() {
while(true) {
bit.setPixel(x++, y++, Color.BLACK);
img.setImageBitmap(bit);
img.invalidate();
try{
Thread.sleep(100);
}catch (Exception e){
}
}
}
});
}
}
Use postDelayed into your ImageView.post() method. Here I have done this on my machine. Plus don't use while(true) stop it when your pixels ends.
img = (ImageView) findViewById(R.id.image);
bit = Bitmap.createBitmap(300, 400, RGB_565);
for(int i = 0; i < 300; i++){
for(int j = 0; j < 400; j++)
bit.setPixel(i,j, Color.WHITE);
}
img.setImageBitmap(bit);
img.post(new Runnable() {
#Override
public void run() {
bit.setPixel(x, y, Color.BLACK);
img.setImageBitmap(bit);
if(++x < 300 && ++y < 400){
img.postDelayed(this, 100);
}
}
});
Try to update View using Handler.
img.post(new Runnable() {
#Override
public void run() {
}
});

Android: How to generate unique random numbers in an array and perform bubble sort?

The following is my code but it doesn't generate unique random numbers
Random rand = new Random();
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = rand.nextInt(n)+1;
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
public TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView);
Random r = new Random();
StringBuffer temp =new StringBuffer("Random numbers:");
for(int i=0;i<10;i++) {
int i1 = r.nextInt(100 - 0) + 0;
temp.append(String.valueOf(i1));
temp.append(String.valueOf(" "));
}
tv.setText(temp.toString());
}
//here ,,I generate 10 random numbers and save it in to stringBuffer and dispaly it in to textView..here range is up to 0 to 100...you can take your own Range ...I hope ,,It will help you...
check this out!... it works for me
Random rand = new Random();
int n = 10;
int[] array = new int[n];
for (int i = 0; i < n; i++){
array[i] = (int) (rand.nextDouble() * n + 1);
}
for (int i = 0; i < ( n - 1 ); i++)
{
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j+1]){
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
}
for (int piece: array) {
System.out.println(piece);
}

Error in Android application AsyncTask

public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new asyncExample().execute("Hello");
}
private class asyncExample extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
System.out.print("Enter number of rows in A: ");
int rowsInA = 10;
System.out.print("Enter number of columns in A / rows in B: ");
int columnsInA = 10;
System.out.print("Enter number of columns in B: ");
int columnsInB = 10;
int[][] a = new int[rowsInA][columnsInA];
int[][] b = new int[columnsInA][columnsInB];
System.out.println("Enter matrix A");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
a[i][j] = 10;
}
}
System.out.println("Enter matrix B");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
b[i][j] = 10;
}
}
int[][] c = multiply(a, b);
System.out.println("Product of A and B is");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
return null;
}
}
public int[][] multiply(int[][] a, int[][] b) {
int rowsInA = a.length;
int columnsInA = a[0].length; // same as rows in B
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
#Override
protected void onPostExecute(String result) {
}
}
I am new to Android and Googled a lot but not getting the context. I am performing matrix multiplication of very large number using AsyncTask but getting error. Any help will be appreciated.
Error:(77, 13) error: method does not override or implement a method from a supertype
add the onPostExecute inside the AsyncTask Class.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new asyncExample().execute("Hello");
}
private class asyncExample extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
System.out.print("Enter number of rows in A: ");
int rowsInA = 10;
System.out.print("Enter number of columns in A / rows in B: ");
int columnsInA = 10;
System.out.print("Enter number of columns in B: ");
int columnsInB = 10;
int[][] a = new int[rowsInA][columnsInA];
int[][] b = new int[columnsInA][columnsInB];
System.out.println("Enter matrix A");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[0].length; j++) {
a[i][j] = 10;
}
}
System.out.println("Enter matrix B");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[0].length; j++) {
b[i][j] = 10;
}
}
int[][] c = multiply(a, b);
System.out.println("Product of A and B is");
for (int i = 0; i < c.length; i++) {
for (int j = 0; j < c[0].length; j++) {
System.out.print(c[i][j] + " ");
}
System.out.println();
}
return null;
}
public int[][] multiply(int[][] a, int[][] b) {
int rowsInA = a.length;
int columnsInA = a[0].length; // same as rows in B
int columnsInB = b[0].length;
int[][] c = new int[rowsInA][columnsInB];
for (int i = 0; i < rowsInA; i++) {
for (int j = 0; j < columnsInB; j++) {
for (int k = 0; k < columnsInA; k++) {
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
return c;
}
#Override
protected void onPostExecute(String result) {
}
}
}
you can add the method onPostExecute() inside the AsyncTask.

Categories

Resources