mercredi 14 novembre 2012

Android Solution Logs

Get a bitmap from an ImageView (StackOverflow)
imageView.setDrawingCacheEnabled(true);
imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), 
                   MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
imageView.layout(0, 0, imageView.getMeasuredWidth(), imageView.getMeasuredHeight()); 
imageView.buildDrawingCache(true);
Bitmap bmap = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false);

Display the soft keyboard (StackOverflow)
// show keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
// hide keyboard
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(),0); 

Check whether the software keyboard is displayed (StackOverflow)
final View activityRootView = findViewById(R.id.activityRoot);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
    Rect r = new Rect();
    //r will be populated with the coordinates of your view that area still visible.
    activityRootView.getWindowVisibleDisplayFrame(r);

    int heightDiff = activityRootView.getRootView().getHeight() - (r.bottom - r.top);
    if (heightDiff > 100) { // if more than 100 pixels, its probably a keyboard...
        ... do something here
    }
 }
});

Get the root view of current activity (StackOverflow)
View rootView = getWindow().getDecorView().findViewById(android.R.id.content)

Canvas drawing example on Android, link.
Draw text on canvas with a custom font (StackOverflow)
Paint paint = new Paint(); 
paint.setStyle(Paint.Style.FILL);     
paint.setColor(Color.WHITE); 
paint.setTextSize(30);
Typeface tf = Typeface.create("Helvetica", Typeface.BOLD);
paint.setTypeface(tf);     
Canvas canvas = new Canvas(bitmap);     
canvas.drawText(text, x, y, paint);

Gesture event detection (snippets)
  • Implements android.view.GestureDetector.OnGestureListener interface
  • Create a new GestureDetector instance: detector = new  GestureDetector(this, this)
  • Call detector.onTouchEvent(event) on the activity onTouchEvent(event)
Detecting touch event (Android Coding).

Display action bar (StackOverflow)

Capture a frame from a video (change since API 14
protected Bitmap videoFrame(String uriString, long msec) {  
  MediaMetadataRetriever retriever = new MediaMetadataRetriever();
  try {                         
     if (Build.VERSION.SDK_INT >= 14) {       
        Map<String, String> headers = new HashMap<String, String>();
        retriever.setDataSource(uriString, headers);
     }else {
        Uri uri = Uri.parse(uriString);
        retriever.setDataSource(context, uri);
     }                     
     return retriever.getFrameAtTime(msec);
  } catch (Exception ex) {
         ex.printStackTrace();
  } finally {
     try {
        retriever.release();
     } catch (RuntimeException ex) {
     }
  }
  return null;
}

Share content via intents (tutorial), sharing image (StackOverflow), set title (StackOverflow)
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));

Customized looking TabActivity (link).

Use Intent share to invoke a Twitter application to post text+image (StackOverflow)
private void share(String nameApp, String imagePath) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
            Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
            targetedShare.setType("image/jpeg"); // put here your mime type

            if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || 
                    info.activityInfo.name.toLowerCase().contains(nameApp)) {
                targetedShare.putExtra(Intent.EXTRA_TEXT,     "My body of post/email");
                targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
                targetedShare.setPackage(info.activityInfo.packageName);
                targetedShareIntents.add(targetedShare);
            }
        }

        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), "Select app to share");
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
        startActivity(chooserIntent);
    }
}

Display pictures of a specific folder in the Gallery app (StackOverflow)
To be continued.

Aucun commentaire:

Enregistrer un commentaire