Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * FFmpegMediaPlayer: A unified interface for playing audio files and streams.
 *
 * Copyright 2014 William Seemann
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap getDefaultArtwork(Context context, int id, int w, int h) {
        BitmapFactory.Options sBitmapOptionsCache = new BitmapFactory.Options();
        Bitmap b = null;
        int sampleSize = 1;

        sBitmapOptionsCache.inPreferredConfig = Bitmap.Config.ARGB_8888;

        // Compute the closest power-of-two scale factor 
        // and pass that to sBitmapOptionsCache.inSampleSize, which will
        // result in faster decoding and better quality
        sBitmapOptionsCache.inJustDecodeBounds = true;

        BitmapFactory.decodeResource(context.getResources(), id, sBitmapOptionsCache);
        int nextWidth = sBitmapOptionsCache.outWidth >> 1;
        int nextHeight = sBitmapOptionsCache.outHeight >> 1;
        while (nextWidth > w && nextHeight > h) {
            sampleSize <<= 1;
            nextWidth >>= 1;
            nextHeight >>= 1;
        }

        sBitmapOptionsCache.inSampleSize = sampleSize;
        sBitmapOptionsCache.inJustDecodeBounds = false;
        b = BitmapFactory.decodeResource(context.getResources(), id, sBitmapOptionsCache);

        if (b != null) {
            // finally rescale to exactly the size we need
            if (sBitmapOptionsCache.outWidth != w || sBitmapOptionsCache.outHeight != h) {
                Bitmap tmp = Bitmap.createScaledBitmap(b, w, h, true);
                // Bitmap.createScaledBitmap() can return the same bitmap
                if (tmp != b)
                    b.recycle();
                b = tmp;
            }
        }

        return b;
    }
}