vendor\mediatek\proprietary\packages\apps\WallpaperPicker\src\com\android\wallpaperpicker\tileinfo\ WallpaperPickerActivity.java是壁纸的选择页面,点击默认壁纸会调用DefaultWallpaperInfo.java 中的onClick方法
@Override
41 public void onClick(WallpaperPickerActivity a) {
42 a.setCropViewTileSource(null, false, false, new CropViewScaleAndOffsetProvider() {
43
44 @Override
45 public float getScale(Point wallpaperSize, RectF crop) {
46 return 1f;
47 }
48
49 @Override
50 public float getParallaxOffset() {
51 return 0.5f;
52 }
53 }, null);
54 }
然后调用WallpaperPickerActivity的父类WallpaperCropActivity.java中的setCropViewTileSource()方法,然后发送MSG_LOAD_IMAGE消息,进入handleMessage方法,然后调用里面的onLoadRequestComplete方法
protected void onLoadRequestComplete(LoadRequest req, boolean success) {
268 mCurrentLoadRequest = null;
269 if (success) {
270 TileSource oldSrc = mCropView.getTileSource();
271 mCropView.setTileSource(req.result, null);
272 mCropView.setTouchEnabled(req.touchEnabled);
273 if (req.moveToLeft) {
274 mCropView.moveToLeft();
275 }
276 if (req.scaleAndOffsetProvider != null) {
277 TileSource src = req.result;
278 Point wallpaperSize = WallpaperUtils.getDefaultWallpaperSize(
279 getResources(), getWindowManager());
280 RectF crop = Utils.getMaxCropRect(src.getImageWidth(), src.getImageHeight(),
281 wallpaperSize.x, wallpaperSize.y, false /* leftAligned */);
282 mCropView.setScale(req.scaleAndOffsetProvider.getScale(wallpaperSize, crop));
283 mCropView.setParallaxOffset(req.scaleAndOffsetProvider.getParallaxOffset(), crop);
284 }
285
286 // Free last image
287 if (oldSrc != null) {
288 // Call yield instead of recycle, as we only want to free GL resource.
289 // We can still reuse the bitmap for decoding any other image.
290 oldSrc.getPreview().yield();
291 }
292 addReusableBitmap(oldSrc);
293 }
294 if (req.postExecute != null) {
295 req.postExecute.run();
296 }
297 mProgressView.setVisibility(View.GONE);
298 }
可以看到会进入if (req.scaleAndOffsetProvider != null) 的判断,里面有关于mCropView.setScale的缩放方法,这个是关键点。它调用的是DefaultWallpaperInfo的setScale()方法,根据里面的代码可以看得出来,没做任何处理,直接默认的返回1,是没做缩放适配屏幕的。
所以修改为获取屏幕尺寸,计算出缩放大小:
vendor/mediatek/proprietary/packages/apps/WallpaperPicker/src/com/android/wallpaperpicker/WallpaperCropActivity.java
@@ -52,6 +52,8 @@ import com.android.wallpaperpicker.common.InputStreamProvider;
import java.util.Collections;
import java.util.Set;
import java.util.WeakHashMap;
+import android.view.WindowManager;
+import android.view.WindowMetrics;
public class WallpaperCropActivity extends Activity implements Handler.Callback {
private static final String LOGTAG = "WallpaperCropActivity";
@@ -163,6 +165,7 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback
final boolean loadSuccess;
if (req.src == null) {
+ Log.d(LOGTAG,"eq.src == null");
Drawable defaultWallpaper = WallpaperManager.getInstance(this)
.getBuiltInDrawable(mCropView.getWidth(), mCropView.getHeight(),
false, 0.5f, 0.5f);
@@ -176,6 +179,7 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback
defaultWallpaper, DrawableTileSource.MAX_PREVIEW_SIZE);
}
} else {
+ Log.d(LOGTAG,"eq.src != null");
try {
req.src.loadInBackground(new InBitmapProvider() {
@@ -224,8 +228,10 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback
@Override
public void run() {
if (req == mCurrentLoadRequest) {
+ Log.d(LOGTAG,"onLoadRequestComplete");
onLoadRequestComplete(req, loadSuccess);
} else {
+ Log.d(LOGTAG,"addReusableBitmap");
addReusableBitmap(req.result);
}
}
@@ -279,6 +285,19 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback
getResources(), getWindowManager());
RectF crop = Utils.getMaxCropRect(src.getImageWidth(), src.getImageHeight(),
wallpaperSize.x, wallpaperSize.y, false /* leftAligned */);
+
+ int width = 0;
+ int height = 0;
+ if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
+ WindowMetrics currentWindowMetrics = getWindowManager().getCurrentWindowMetrics();
+ width = currentWindowMetrics.getBounds().width();
+ height = currentWindowMetrics.getBounds().height();
+ }else {
+ width = getWindowManager().getDefaultDisplay().getWidth();
+ height = getWindowManager().getDefaultDisplay().getHeight();
+ }
+ wallpaperSize.set(width,height);
+ Log.d(LOGTAG,"onLoadRequestComplete src.getImageWidth()="+src.getImageWidth()+",src.getImageHeight()="+src.getImageHeight()+",wallpaperSize.x="+wallpaperSize.x+",wallpaperSize.y="+wallpaperSize.y);
mCropView.setScale(req.scaleAndOffsetProvider.getScale(wallpaperSize, crop));
mCropView.setParallaxOffset(req.scaleAndOffsetProvider.getParallaxOffset(), crop);
}
@@ -296,6 +315,7 @@ public class WallpaperCropActivity extends Activity implements Handler.Callback
}
mProgressView.setVisibility(View.GONE);
}
+
vendor/mediatek/proprietary/packages/apps/WallpaperPicker/src/com/android/wallpaperpicker/tileinfo/DefaultWallpaperInfo.java
+import android.util.Log;
+
public class DefaultWallpaperInfo extends DrawableThumbWallpaperInfo {
private static final String TAG = "DefaultWallpaperInfo";
@@ -43,12 +45,14 @@ public class DefaultWallpaperInfo extends DrawableThumbWallpaperInfo {
@Override
public float getScale(Point wallpaperSize, RectF crop) {
- return 1f;
+ //return 1f;
+ return wallpaperSize.x /crop.width();
}
@Override
public float getParallaxOffset() {
- return 0.5f;
+ //return 0.5f;
+ return a.getWallpaperParallaxOffset();
}
}, null);
}