Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting imageview size for Bitmap creation

I am a programmer with a Windows background and I am new to Java and Android stuff.

I want to create a widget (not an app) which displays a chart. After a long research I know I can do this with Canvas, imageviews and Bitmaps. The canvas which I paint on should be the same as the Widget Size.

How do I know the widget size (or imageview size) so that I can supply it to the function?

Bitmap.createBitmap(width_xx, height_yy, Config.ARGB_8888);

Code Snippet:

In the timer run method:

@Override
public void run() {
    Bitmap bitmap = Bitmap.createBitmap(??, ??, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    // Create a new paint
    Paint p = new Paint();
    p.setAntiAlias(true);
    p.setStrokeWidth(1);

    // Draw circle
    // Here I can use the width and height to scale the circle
    canvas.drawCircle(50, 50, 7, p);
    remoteViews.setImageViewBitmap(R.id.imageView, bitmap);
like image 436
Augustus Avatar asked Feb 26 '12 18:02

Augustus


People also ask

How do I get bitmap from ImageView?

Bitmap bm=((BitmapDrawable)imageView. getDrawable()). getBitmap(); Try having the image in all drawable qualities folders (drawable-hdpi/drawable-ldpi etc.)

What is an ImageView?

An ImageView control is used to display images in Android applications. An image can be displayed by assigning it to the ImageView control and including the android:src attribute in the XML definition of the control. Images can also be dynamically assigned to the ImageView control through Java code.

How can you tell the size of a photo on android?

On android go to photos, select your photo and click the ... in the top right. Scroll to bottom of page to find image size.


1 Answers

From what I've learnt, you can only calculate widget dimensions on Android 4.1+. When on a lower API, you'll have to use static dimensions. About widget dimensions: App Widget Design Guidelines

int w = DEFAULT_WIDTH, h = DEFAULT_HEIGHT;

if ( Build.VERSION.SDK_INT >= 16 ) {
    Bundle options = appWidgetManager.getAppWidgetOptions(widgetId);

    int maxW = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH);
    int maxH = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT);

    int minW = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH);
    int minH = options.getInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT);

    if ( context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE ) {
        w = maxW;
        h = minH;
    } else {
        w = minW;
        h = maxH;
    }
}
like image 170
Christian Göllner Avatar answered Oct 22 '22 10:10

Christian Göllner