【问题标题】:How to fix Profile image like instagram in android如何在android中修复像instagram这样的个人资料图片
【发布时间】:2015-04-21 16:55:58
【问题描述】:

我正在开发一个需要像 instagram 这样的固定个人资料图像的应用程序。这可能吗? 如果有人有想法请帮助...

【问题讨论】:

标签: android imageview


【解决方案1】:
//this is java class
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.widget.ImageView;

public class ImageViewRounded extends ImageView {

    public ImageViewRounded(Context context) {
        super(context);
    }

    public ImageViewRounded(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ImageViewRounded(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        BitmapDrawable drawable = (BitmapDrawable) getDrawable();

        if (drawable == null) {
            return;
        }

        if (getWidth() == 0 || getHeight() == 0) {
            return;
        }

        Bitmap fullSizeBitmap = drawable.getBitmap();

        int scaledWidth = getMeasuredWidth();
        int scaledHeight = getMeasuredHeight();

        Bitmap mScaledBitmap;
        if (scaledWidth == fullSizeBitmap.getWidth()
                && scaledHeight == fullSizeBitmap.getHeight()) {
            mScaledBitmap = fullSizeBitmap;
        } else {
            mScaledBitmap = Bitmap.createScaledBitmap(fullSizeBitmap,
                scaledWidth, scaledHeight, true /* filter */);
        }

        // Bitmap roundBitmap = getRoundedCornerBitmap(mScaledBitmap);

        // Bitmap roundBitmap = getRoundedCornerBitmap(getContext(),
        // mScaledBitmap, 10, scaledWidth, scaledHeight, false, false,
        // false, false);
        // canvas.drawBitmap(roundBitmap, 0, 0, null);

        Bitmap circleBitmap = getCircledBitmap(mScaledBitmap);

    canvas.drawBitmap(circleBitmap, 0, 0, null);

    }

    public Bitmap getRoundedCornerBitmap(Context context, Bitmap input,
        int pixels, int w, int h, boolean squareTL, boolean squareTR,
        boolean squareBL, boolean squareBR) {

        Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final float densityMultiplier = context.getResources()
            .getDisplayMetrics().density;

        final int color = 0xff424242;

        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, w, h);
        final RectF rectF = new RectF(rect);

        // make sure that our rounded corner is scaled appropriately
        final float roundPx = pixels * densityMultiplier;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        // draw rectangles over the corners we want to be square
        if (squareTL) {
            canvas.drawRect(0, 0, w / 2, h / 2, paint);
        }
        if (squareTR) {
            canvas.drawRect(w / 2, 0, w, h / 2, paint);
        }
        if (squareBL) {
            canvas.drawRect(0, h / 2, w / 2, h, paint);
        }
        if (squareBR) {
            canvas.drawRect(w / 2, h / 2, w, h, paint);
        }

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(input, 0, 0, paint);

        return output;
    }

    Bitmap getCircledBitmap(Bitmap bitmap) {

        Bitmap result = Bitmap.createBitmap(bitmap.getWidth(),
            bitmap.getHeight(), Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(result);

        int color = Color.BLUE;
        Paint paint = new Paint();
        Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
            bitmap.getHeight() / 2, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return result;
    }

}

//在xml中使用

<yourpackagename.ImageViewRounded
        android:id="@+id/ivProfile"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginTop="30dp"
        android:src="@drawable/face" />

【讨论】:

    【解决方案2】:
    //Use this java file 
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.Bitmap.Config;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.PorterDuff.Mode;
    import android.graphics.PorterDuffXfermode;
    import android.graphics.Rect;
    import android.graphics.drawable.BitmapDrawable;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.widget.ImageView;
    
    public class RoundImage extends ImageView {
        public RoundImage(Context ctx, AttributeSet attrs) {
            super(ctx, attrs);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            Drawable drawable = getDrawable();
            if (drawable == null) {
                return;
            }
            if (getWidth() == 0 || getHeight() == 0) {
                return;
            }
            Bitmap b = ((BitmapDrawable) drawable).getBitmap();
            Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
            int w = getWidth(), h = getHeight();
            Bitmap roundBitmap = getRoundedCroppedBitmap(bitmap, w);
            canvas.drawBitmap(roundBitmap, 0, 0, null);
        }
    
        public static Bitmap getRoundedCroppedBitmap(Bitmap bitmap, int radius) {
            Bitmap finalBitmap;
            if (bitmap.getWidth() != radius || bitmap.getHeight() != radius)
                finalBitmap = Bitmap.createScaledBitmap(bitmap, radius, radius,
                        false);
            else
                finalBitmap = bitmap;
            Bitmap output = Bitmap.createBitmap(finalBitmap.getWidth(),
                    finalBitmap.getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(output);
            final Paint paint = new Paint();
            final Rect rect = new Rect(0, 0, finalBitmap.getWidth(),
                    finalBitmap.getHeight());
            paint.setAntiAlias(true);
            paint.setFilterBitmap(true);
            paint.setDither(true);
            canvas.drawARGB(0, 0, 0, 0);
            paint.setColor(Color.parseColor("#BAB399"));
            canvas.drawCircle(finalBitmap.getWidth() / 2 + 0.7f,
                    finalBitmap.getHeight() / 2 + 0.7f,
                    finalBitmap.getWidth() / 2 + 0.1f, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
            canvas.drawBitmap(finalBitmap, rect, rect, paint);
            return output;
        }
    }
    

    现在,在您的 xml 布局中更改 imageview,如下所示:

                   <com.packagename.RoundImage
                    android:id="@+id/profile_icon"
                    android:layout_width="70dp"
                    android:layout_height="70dp"
                    android:layout_gravity="center_vertical"
                    android:padding="5dp"
                    android:src="@drawable/default_image_male" />
    

    现在,在活动中,您必须使用 RoundImage 而不是 ImageView。

    【讨论】:

    • 这里你的答案只适用于roundimage..但在这里我想修复配置文件图像。即不是每次都加载..
    • 你想从哪里加载图片库、相机捕捉或服务器?
    • 从这里下载毕加索库 - square.github.io/picasso,然后导入到您的项目中。导入后使用此代码将 url 加载到 imageview " Picasso.with(this) .load("url") .fit().into(imageview); "
    • 实际上我在片段中使用了它..在片段中我第一次想要加载图像..我不想每次调用片段时都加载。我也试过毕加索..但是当我反复调用片段时,图像会反复加载...
    【解决方案3】:

    这可以通过文件缓存来实现。

    //制作这个java文件

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.Collections;
    import java.util.Map;
    import java.util.WeakHashMap;
    import java.util.concurrent.ExecutorService;
    import java.util.concurrent.Executors;
    
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.os.Handler;
    import android.widget.ImageView;
    
    import com.wholesaleraja.R;
    import com.wholesaleraja.adapter.Utils;
    
    public class ImageLoader {
    
        MemoryCache memoryCache = new MemoryCache();
        FileCache fileCache;
        private Map<ImageView, String> imageViews = Collections
                .synchronizedMap(new WeakHashMap<ImageView, String>());
        ExecutorService executorService;
        // Handler to display images in UI thread
        Handler handler = new Handler();
    
        public ImageLoader(Context context) {
            fileCache = new FileCache(context);
            executorService = Executors.newFixedThreadPool(5);
        }
    
        final int stub_id = R.drawable.raja;
    
        public void DisplayImage(String url, ImageView imageView) {
    
            imageViews.put(imageView, url);
            Bitmap bitmap = memoryCache.get(url);
            if (bitmap != null)
                imageView.setImageBitmap(bitmap);
            else {
                queuePhoto(url, imageView);
                imageView.setImageResource(stub_id);
            }
        }
    
        private void queuePhoto(String url, ImageView imageView) {
            PhotoToLoad p = new PhotoToLoad(url, imageView);
            executorService.submit(new PhotosLoader(p));
        }
    
        private Bitmap getBitmap(String url) {
            File f = fileCache.getFile(url);
    
            Bitmap b = decodeFile(f);
            if (b != null)
                return b;
    
            // Download Images from the Internet
            try {
                Bitmap bitmap = null;
                URL imageUrl = new URL(url);
                HttpURLConnection conn = (HttpURLConnection) imageUrl
                        .openConnection();
                conn.setConnectTimeout(30000);
                conn.setReadTimeout(30000);
                conn.setInstanceFollowRedirects(true);
                InputStream is = conn.getInputStream();
                OutputStream os = new FileOutputStream(f);
                Utils.CopyStream(is, os);
                os.close();
                conn.disconnect();
                bitmap = decodeFile(f);
                return bitmap;
            } catch (Throwable ex) {
                ex.printStackTrace();
                if (ex instanceof OutOfMemoryError)
                    memoryCache.clear();
                return null;
            }
        }
    
        // Decodes image and scales it to reduce memory consumption
        private Bitmap decodeFile(File f) {
            try {
                // Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                FileInputStream stream1 = new FileInputStream(f);
                BitmapFactory.decodeStream(stream1, null, o);
                stream1.close();
    
                // Find the correct scale value. It should be the power of 2.
                // Recommended Size 512
                final int REQUIRED_SIZE = 70;
                int width_tmp = o.outWidth, height_tmp = o.outHeight;
                int scale = 1;
                while (true) {
                    if (width_tmp / 2 < REQUIRED_SIZE
                            || height_tmp / 2 < REQUIRED_SIZE)
                        break;
                    width_tmp /= 2;
                    height_tmp /= 2;
                    scale *= 2;
                }
    
                // Decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                FileInputStream stream2 = new FileInputStream(f);
                Bitmap bitmap = BitmapFactory.decodeStream(stream2, null, o2);
                stream2.close();
                return bitmap;
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }
    
        // Task for the queue
        private class PhotoToLoad {
            public String url;
            public ImageView imageView;
    
            public PhotoToLoad(String u, ImageView i) {
                url = u;
                imageView = i;
            }
        }
    
        class PhotosLoader implements Runnable {
            PhotoToLoad photoToLoad;
    
            PhotosLoader(PhotoToLoad photoToLoad) {
                this.photoToLoad = photoToLoad;
            }
    
            @Override
            public void run() {
                try {
                    if (imageViewReused(photoToLoad))
                        return;
                    Bitmap bmp = getBitmap(photoToLoad.url);
                    memoryCache.put(photoToLoad.url, bmp);
                    if (imageViewReused(photoToLoad))
                        return;
                    BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad);
                    handler.post(bd);
                } catch (Throwable th) {
                    th.printStackTrace();
                }
            }
        }
    
        boolean imageViewReused(PhotoToLoad photoToLoad) {
            String tag = imageViews.get(photoToLoad.imageView);
            if (tag == null || !tag.equals(photoToLoad.url))
                return true;
            return false;
        }
    
        // Used to display bitmap in the UI thread
        class BitmapDisplayer implements Runnable {
            Bitmap bitmap;
            PhotoToLoad photoToLoad;
    
            public BitmapDisplayer(Bitmap b, PhotoToLoad p) {
                bitmap = b;
                photoToLoad = p;
            }
    
            public void run() {
                if (imageViewReused(photoToLoad))
                    return;
                if (bitmap != null)
                    photoToLoad.imageView.setImageBitmap(bitmap);
                else
                    photoToLoad.imageView.setImageResource(stub_id);
            }
        }
    
        public void clearCache() {
            memoryCache.clear();
            fileCache.clear();
        }
    
    }
    

    //为缓存创建另一个java文件

    import java.io.File;
    
    import android.content.Context;
    import android.util.Log;
    
    public class FileCache {
        private File cacheDir;
        private static final String Main_DIRECTORY_NAME = "File";
        private static final String Cache_DIRECTORY_NAME = "_cache";
    
        public FileCache(Context context) {
            // Find the dir to save cached images
            if (android.os.Environment.getExternalStorageState().equals(
                    android.os.Environment.MEDIA_MOUNTED))
                cacheDir = new File(
                        android.os.Environment
                                .getExternalStoragePublicDirectory(Main_DIRECTORY_NAME),
                        Cache_DIRECTORY_NAME);
            else
                cacheDir = context.getCacheDir();
            // if (!cacheDir.exists())
            // cacheDir.mkdirs();
    
            if (!cacheDir.exists()) {
                if (!cacheDir.mkdirs()) {
                    Log.d(Cache_DIRECTORY_NAME, "Oops! Failed create "
                            + Cache_DIRECTORY_NAME + " directory");
    
                }
            }
        }
    
        public File getFile(String url) {
            String filename = String.valueOf(url.hashCode());
            // String filename = URLEncoder.encode(url);
            File f = new File(cacheDir, filename);
            return f;
    
        }
    
        public void clear() {
            File[] files = cacheDir.listFiles();
            if (files == null)
                return;
            for (File f : files)
                f.delete();
        }
    }
    

    //为内存缓存制作另一个java文件

    import java.util.Collections;
    import java.util.Iterator;
    import java.util.LinkedHashMap;
    import java.util.Map;
    import java.util.Map.Entry;
    
    import android.graphics.Bitmap;
    import android.util.Log;
    
    public class MemoryCache {
        private static final String TAG = "MemoryCache";
    
        // Last argument true for LRU ordering
        private Map<String, Bitmap> cache = Collections
                .synchronizedMap(new LinkedHashMap<String, Bitmap>(10, 1.5f, true));
    
        // Current allocated size
        private long size = 0;
    
        // Max memory in bytes
        private long limit = 5000000;
    
        public MemoryCache() {
            // Use 25% of available heap size
            setLimit(Runtime.getRuntime().maxMemory() / 4);
        }
    
        public void setLimit(long new_limit) {
            limit = new_limit;
            //Log.i(TAG, "MemoryCache will use up to " + limit / 1024. / 1024. + "MB");
        }
    
        public Bitmap get(String id) {
            try {
                if (!cache.containsKey(id))
                    return null;
                return cache.get(id);
            } catch (NullPointerException ex) {
                ex.printStackTrace();
                return null;
            }
        }
    
        public void put(String id, Bitmap bitmap) {
            try {
                if (cache.containsKey(id))
                    size -= getSizeInBytes(cache.get(id));
                cache.put(id, bitmap);
                size += getSizeInBytes(bitmap);
                checkSize();
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    
        private void checkSize() {
            //Log.i(TAG, "cache size=" + size + " length=" + cache.size());
            if (size > limit) {
                // Least recently accessed item will be the first one iterated
                Iterator<Entry<String, Bitmap>> iter = cache.entrySet().iterator();
                while (iter.hasNext()) {
                    Entry<String, Bitmap> entry = iter.next();
                    size -= getSizeInBytes(entry.getValue());
                    iter.remove();
                    if (size <= limit)
                        break;
                }
                //Log.i(TAG, "Clean cache. New size " + cache.size());
            }
        }
    
        public void clear() {
            try {
                cache.clear();
                size = 0;
            } catch (NullPointerException ex) {
                ex.printStackTrace();
            }
        }
    
        long getSizeInBytes(Bitmap bitmap) {
            if (bitmap == null)
                return 0;
            return bitmap.getRowBytes() * bitmap.getHeight();
        }
    }
    

    //做完这一切之后,现在你必须调用图像加载器类文件

    ImageLoader imageloader = new ImageLoader(this);
        imageLoader.DisplayImage("url",
                    imageview);
    

    【讨论】:

    • 我试过了,但没用..这也需要一些时间来将图像加载到 imageview 中。
    • 然后你可以从url下载图片并保存到sdcard中,下次取下载的路径并从路径设置图片。
    • 我认为这也需要一些时间。你在 Instagram 上观察个人资料吗?其实我的要求是这样的..它应该是固定的..
    • 没关系...除了我不知道的,我知道我已经给出了什么。
    • 欢迎您,如果您找到了解决方案,也请告诉我。:)
    【解决方案4】:

    毕加索图书馆是处理个人资料图片的最常见做法

    毕加索同时处理圆形图像和图像缓存。如果您希望图像在没有连接的情况下仍然存在,您可以执行以下操作:

    此方法将位图图像保存到本地应用程序文件夹中名为 userphotos 的目录中

     private void saveToInternalStorage(Bitmap bitmapImage, String userID){
    
            File image;
            ContextWrapper cw = new ContextWrapper(context);
            File directory = cw.getDir("userphotos", Context.MODE_PRIVATE); //Mode Private will overwrites file if exists
    
            mage = new File(directory, userID + ".jpg");
    
            FileOutputStream fos;
    
            try {
                fos = new FileOutputStream(image);
                bitmapImage.compress(Bitmap.CompressFormat.JPEG, 75, fos);
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
    

    }

    以下方法从本地文件夹加载图像

     public static Bitmap getPhoto(Context context, String userID){
    
        Bitmap photo = null;
    
        File image;
        ContextWrapper cw = new ContextWrapper(context);
        File directory = cw.getDir("userphotos", Context.MODE_PRIVATE);
    
        image = new File(directory, userID + ".jpg");
    
        if(image.exists()){
    
            try {
                FileInputStream fi = new FileInputStream(image);
                photo = BitmapFactory.decodeStream(fi);
                return photo;
            } catch (Exception ex) {
            }
    
    
        }
    
        return null;
    

    }

    现在你只需要从设备加载图像,如果互联网可用,你可以使用以下方法检查

     public static boolean isNetworkAvailable(Context context) {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    }
    

    如果互联网可用,您可以使用 Picasso 加载图像。这里有 2 个选项...

    1. 您可以将图像加载到您的 imageview 中,这样您就可以从淡入淡出的动画和缓存中受益,然后使用 picasso 再次加载图像,但不是将位图放入 imageview 中,而是将位图保存到设备文件夹中我发布的第一种方法。

    2. 您从驱动器加载图像,然后毕加索抓取图像并将其放入设备文件夹而不是图像视图。

    毕加索..

    获取带有毕加索路径的图像文件:

    public static File getImageFilePath(Context context, String userID){
    
        File imagePath;
        ContextWrapper cw = new ContextWrapper(context);
        File directory = cw.getDir(PHOTOS_DIR, Context.MODE_PRIVATE);
    
        imagePath = new File(directory, userID + ".jpg");
    
    
        if(imagePath.exists()){
    
            return imagePath;
    
        }else{
            return null;
        }
    
    }
    

    然后用毕加索加载:

    File filePath = getImageFilePath(context, userID);
    
    Picasso.with(ctx).load(filePath).into(imageView);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      相关资源
      最近更新 更多