Skip to content

Commit

Permalink
第一次提交,完成异步加载、并发加载时图片错位、内存缓存
Browse files Browse the repository at this point in the history
  • Loading branch information
XinYiWorld committed Nov 8, 2015
0 parents commit de0b842
Show file tree
Hide file tree
Showing 33 changed files with 1,108 additions and 0 deletions.
76 changes: 76 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/# Built application files
*.apk
*.ap_

# Files for the Dalvik VM
*.dex

# Java class files
*.class

# Generated files
bin/
gen/

# Gradle files
.gradle/
build/

# Local configuration file (sdk path, etc)
local.properties

# Proguard folder generated by Eclipse
proguard/

# Log Files
*.log

# Android Studio Navigation editor temp files
.navigation/captures

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio

*.iml

## Directory-based project format:
.idea/
# if you remove the above rule, at least ignore the following:

# User-specific stuff:
# .idea/workspace.xml
# .idea/tasks.xml
# .idea/dictionaries

# Sensitive or high-churn files:
# .idea/dataSources.ids
# .idea/dataSources.xml
# .idea/sqlDataSources.xml
# .idea/dynamic.xml
# .idea/uiDesigner.xml

# Gradle:
# .idea/gradle.xml
# .idea/libraries

# Mongo Explorer plugin:
# .idea/mongoSettings.xml

## File-based project format:
*.ipr
*.iws

## Plugin-specific files:

# IntelliJ
/out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
27 changes: 27 additions & 0 deletions ImageLoadHelperByXinYi/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.1"

defaultConfig {
applicationId "bitmap.trainning.google.com.displaybitmapsefficiently"
minSdkVersion 10
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.0'
compile 'com.google.guava:guava:18.0'
}
17 changes: 17 additions & 0 deletions ImageLoadHelperByXinYi/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in F:\MyAndroidWorkSpace\EclipseRoot\sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package bitmap.trainning.google.com.displaybitmapsefficiently;

import android.app.Application;
import android.test.ApplicationTestCase;

/**
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
*/
public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
21 changes: 21 additions & 0 deletions ImageLoadHelperByXinYi/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bitmap.trainning.google.com.displaybitmapsefficiently" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity android:name=".ui.MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package bitmap.trainning.google.com.displaybitmapsefficiently.ui;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;

import bitmap.trainning.google.com.displaybitmapsefficiently.R;
import bitmap.trainning.google.com.displaybitmapsefficiently.util.core.ImageDownloader;

public class MainActivity extends Activity {

private ImageView mImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mImageView = (ImageView) findViewById(R.id.mImageView);

/* // 界面渲染结束后设置进去, 可获取高度
mImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 视图树, 界面渲染结束时被调用
String s = MoreObjects.toStringHelper(this)
.add("width", mImageView.getWidth())
.add("height", mImageView.getHeight())
.toString();
Log.i("cz", s);
BitmapScaleUtils.loadBitmap(getBaseContext(), R.mipmap.lxs, mImageView);
mImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});*/

/**
* example url :
* http://e.hiphotos.baidu.com/image/pic/item/d788d43f8794a4c201bd81230cf41bd5ad6e391f.jpg
* http://c.hiphotos.baidu.com/image/h%3D200/sign=08523c111fd5ad6eb5f963eab1ca39a3/377adab44aed2e73519d81f98301a18b86d6faeb.jpg
* http://g.hiphotos.baidu.com/image/h%3D360/sign=f0187d289d510fb367197191e932c893/b999a9014c086e060b400cda01087bf40bd1cbb2.jpg
*/
ImageDownloader.display(this,"http://g.hiphotos.baidu.com/image/h%3D360/sign=f0187d289d510fb367197191e932c893/b999a9014c086e060b400cda01087bf40bd1cbb2.jpg", mImageView);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package bitmap.trainning.google.com.displaybitmapsefficiently.util.core;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;

/**
* Created by LENOVO on 2015/11/6.
* come from "http://developer.android.com/intl/zh-cn/training/displaying-bitmaps/manage-memory.html","http://android-developers.blogspot.com/"
* bug1:the bitmap shows normally on virtual device,but wrongly on real device
*/

public class BitmapScaleUtils {


//calculate the scale percentage
public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}

return inSampleSize; //the inSampleSize is more larger,the more small the bitmap will be.
}

//decode bitmap from resources
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
int reqWidth, int reqHeight) {

// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}

//防止图片的像素过大,对图片进行一定的压缩。
public static Bitmap decodeSampledBitmapFromSelf(Bitmap srcBitmap, int reqWidth, int reqHeight){
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.outWidth = srcBitmap.getWidth();
options.outHeight = srcBitmap.getHeight();
int inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

//make a copy of the Bitmap
Bitmap.Config config = srcBitmap.getConfig();
Bitmap copyBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), config);
Matrix matrix = new Matrix();

//scale the copybitmap,if possible ,make it smaller to avoid the oom
matrix.setScale(1.0f/inSampleSize,1.0f/inSampleSize);
new Canvas(copyBitmap).drawBitmap(srcBitmap, matrix, new Paint());
return copyBitmap;
}
}
Loading

0 comments on commit de0b842

Please sign in to comment.