Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

Compressing converting a bitmap to PNG format Android example


AD MOB


public class MainActivity extends Activity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
			int quality) {
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		src.compress(format, quality, os);

		byte[] array = os.toByteArray();
		return BitmapFactory.decodeByteArray(array, 0, array.length);
	}

	private static class SampleView extends View {

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

		}
		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = new Paint();
			
			canvas.drawColor(Color.YELLOW);
            
        
			Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.flower_blue);
             
			Bitmap bPNGcompress = codec(bmp, Bitmap.CompressFormat.PNG, 0);

		   int h = bmp.getHeight();
           
           canvas.drawBitmap(bmp, 10,10, paint);
           canvas.drawBitmap(bPNGcompress, 10,10 + h + 10, paint);
           
		}

	}
}


397LW NO topic_id




AD

Další témata ....(Topics)


107

Memory usage of application in Android sample code | memory-usage-of-application-in-android-sample-code



public class MainClass extends Activity {
ActivityManager activityManager; 
MemoryInfo memoryInfo; 
final String TAG = "MemInfo";


@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

         activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
         memoryInfo = new ActivityManager.MemoryInfo();

        activityManager.getMemoryInfo(memoryInfo);
 
    	           Log.i(TAG, " memoryInfo.availMem " + memoryInfo.availMem);
    	           Log.i(TAG, " memoryInfo.lowMemory " + memoryInfo.lowMemory);
    	           Log.i(TAG, " memoryInfo.threshold " + memoryInfo.threshold);

  Toast.makeText(getApplicationContext(),  String.valueOf( memoryInfo.availMem), Toast.LENGTH_LONG)
		.show();

    } // end onCreate
}
240

AsyncTask Example Android with ProgressBar | asynctask-example-android-with-progressbar


MainActivity.java

package com.asynctaskexample;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView textView;
	private ProgressBar progressBar;
	private DownloadWebPageTask mTask = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		textView = (TextView) findViewById(R.id.TextView01);
		downloadPage();
	}

	// AsyncTask <TypeOfVarArgParams , ProgressValue , ResultValue> .
	private class DownloadWebPageTask extends AsyncTask<String, Integer, String> {

		@Override
		protected void onPreExecute() {
			//textView.setText("Hello !!!");
			progressBar = (ProgressBar) findViewById(R.id.progressBar1);
			progressBar.setVisibility(View.VISIBLE);
			super.onPreExecute();
		}

		@Override
		protected void onProgressUpdate(Integer... values) {
			super.onProgressUpdate(values);

		}

		@Override
		protected String doInBackground(String... urls) {
			String response = "";
			for (String url : urls) {
				DefaultHttpClient client = new DefaultHttpClient();
				HttpGet httpGet = new HttpGet(url);
				try {
					HttpResponse execute = client.execute(httpGet);
					InputStream content = execute.getEntity().getContent();

					BufferedReader buffer = new BufferedReader(new InputStreamReader(
							content));
					String s = "";
					while ((s = buffer.readLine()) != null) {
						response += s;
					}

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			return response;
		}

		@Override
		protected void onPostExecute(String result) {
			progressBar.setVisibility(View.INVISIBLE);
			textView.setText(result);
		}
	}

	private void downloadPage() {
		if (mTask != null
				&& mTask.getStatus() != DownloadWebPageTask.Status.FINISHED) {
			mTask.cancel(true);
		}
               // execute(String[]) you can put array of links to web pages, or array of Integer[] 
               // if first param is Integer[] etc.
		mTask = (DownloadWebPageTask) new DownloadWebPageTask()
				.execute(new String[] { "//android.okhelp.cz/android-market.html",
						"//android.okhelp.cz/android-market.html" });
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();

		if (mTask != null
				&& mTask.getStatus() != DownloadWebPageTask.Status.FINISHED) {
			mTask.cancel(true);
			mTask = null;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

}




activity_main.xml

<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>



AndroidManifest.xml do not forget INTERNET uses-permission !!!!!!!

<uses-permission android:name="android.permission.INTERNET" />

223

Disable enable internet connection in Android Emulator | disable-enable-internet-connection-in-android-emulator


If you try function for checking internet connection you can disable internet on the emulator:
Settings - Wireless and networks - Mobile networks - Data enabled (checked - unchecked )


 public boolean isNetworkAvailable() {
        ConnectivityManager cm = (ConnectivityManager) 
          getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            return true;
        }
        return false;
    }     





56

Change background color dynamically View TableRow setBackgroundColor Android example | change-background-color-dynamically-view-tablerow-android-example


How change background color of View Android sample.

MainActivity.java

public class MainActivity extends Activity {
TextView hTextView;
TableRow hTableRow;
Button hButton, hButtonStop;
private Handler mHandler = new Handler();
private int nCounter = 0;
@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        hTextView = (TextView)findViewById(R.id.idTextView);
        hButton = (Button)findViewById(R.id.idButton);
        hButton.setOnClickListener(mButtonStartListener);
        hTableRow = (TableRow)findViewById(R.id.idTableRow1);


        
        
    } // end onCreate

 View.OnClickListener mButtonStartListener = new OnClickListener() {
	public void onClick(View v) {
           hTableRow.setBackgroundColor(Color.YELLOW);
	}
 };
}





main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >


<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tableLayout1">
    <TableRow android:id="@+id/idTableRow1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:background="#5655AA">
<TextView  
	android:id="@+id/idTextView"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
    
    </TableRow>
</TableLayout>
</LinearLayout>
153

Rotate Canvas with Bitmap Android example | rotate-canvas-with-bitmap-android-example


drawPath, canvas.rotate, lineTo basic Android example for your testing.





canvas-rotate-image-android-1


canvas-rotate-image-android




// //www.apache.org/licenses/LICENSE-2.0
// The Android Open Source Project
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {
		private Paint mPaint = new Paint();
		private Path mPath = new Path();

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

			// Construct a wedge-shaped path
			mPath.moveTo(0, -60);
			mPath.lineTo(-20, 80);
			mPath.lineTo(0, 60);
			mPath.lineTo(20, 80);
			mPath.close();
		}

		@Override
		protected void onDraw(Canvas canvas) {
			Paint paint = mPaint;

			canvas.drawColor(Color.WHITE);

			paint.setAntiAlias(true);
			paint.setColor(Color.RED);
			paint.setStyle(Paint.Style.FILL);
			Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
					R.drawable.flower_blue);
			canvas.drawBitmap(bitmapOrg, 10, 10, paint);
			int w = canvas.getWidth();
			int h = canvas.getHeight();
			int cx = w / 2;
			int cy = h / 2;

			canvas.translate(cx, cy);
         // uncomment next line 
			//canvas.rotate(90.0f);
			canvas.drawPath(mPath, mPaint);
		}

	}
}


Editace: 2013-12-09 13:09:31
Počet článků v kategorii: 397
Url:compressing-converting-a-bitmap-to-png-format-android-example

AD
11 Share
Share
Tweet