下面列出了android.graphics.drawable.Drawable#createFromPath() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void showStartDialog(AppItem app) {
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
StringBuilder text = new StringBuilder()
.append(getString(R.string.author)).append(' ')
.append(app.getAuthor()).append('\n')
.append(getString(R.string.version)).append(' ')
.append(app.getVersion()).append('\n');
dialog.setMessage(text);
dialog.setTitle(app.getTitle());
Drawable drawable = Drawable.createFromPath(app.getImagePathExt());
if (drawable != null) dialog.setIcon(drawable);
dialog.setPositiveButton(R.string.START_CMD, (d, w) -> {
Config.startApp(getActivity(), app.getPath(), false);
});
dialog.setNegativeButton(R.string.close, null);
dialog.show();
}
/**
* Load background image from the assets directory.
* @return Drawable object representing the background image.
*/
private Drawable loadBackground() {
Resources r = getResources();
Log.d("DroidForce", "resources: "+ r);
String[] files = null;
try {
files = r.getAssets().list("");
} catch (IOException e) {
Log.e("DroidForce", "exception "+ e);
e.printStackTrace();
}
Log.d("DroidForce", "number of files in assets: "+ files.length);
for (String s: files) {
Log.d("DroidForce", "file in asset: "+ s);
if (s.endsWith("protect.png")) {
File file = copyFileFromAssetsToInternalStorage(s, true);
String path = file.getAbsolutePath();
Log.d("DroidForce", "Path to file is '"+ path +"'");
return Drawable.createFromPath(path);
}
}
throw new RuntimeException("error: drawable not found.");
}
private AdapterNativeMappedImage parseImageComponent(final JSONObject jsonObject) {
if (jsonObject != null) {
try {
JSONObject dataObject = jsonObject.getJSONObject("data");
Uri url = Uri.parse(dataObject.optString("url"));
String assetPath = dataObject.optString("asset");
Drawable drawable;
if (TextUtils.isEmpty(assetPath)) {
drawable = drawableFromUrl(url.toString());
} else {
drawable = Drawable.createFromPath(assetPath);
}
return new AdapterNativeMappedImage(drawable, url,
VerizonMediationAdapter.VAS_IMAGE_SCALE);
} catch (Exception e) {
Log.e(TAG, "Unable to parse data object.", e);
}
}
return null;
}
private void addUsersToMenu(ArrayList<Action> items) {
UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
if (um.isUserSwitcherEnabled()) {
List<UserInfo> users = um.getUsers();
UserInfo currentUser = getCurrentUser();
for (final UserInfo user : users) {
if (user.supportsSwitchToByUser()) {
boolean isCurrentUser = currentUser == null
? user.id == 0 : (currentUser.id == user.id);
Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
: null;
SinglePressAction switchToUser = new SinglePressAction(
com.android.internal.R.drawable.ic_menu_cc, icon,
(user.name != null ? user.name : "Primary")
+ (isCurrentUser ? " \u2714" : "")) {
@Override
public void onPress() {
try {
ActivityManager.getService().switchUser(user.id);
} catch (RemoteException re) {
Log.e(TAG, "Couldn't switch user " + re);
}
}
@Override
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return false;
}
};
items.add(switchToUser);
}
}
}
}
/**
* get drawable by path
*
* @param filePath
* @return
*/
public static Drawable getByPath(String filePath) {
Drawable drawable = WeakCache.getCache(TAG).get(filePath);
if (drawable == null) {
try {
drawable = Drawable.createFromPath(filePath);
WeakCache.getCache(TAG).put(filePath, drawable);
} catch (Throwable e) {
}
}
return drawable;
}
/**
* 读取文件到drawable
*
* @param filepath 文件路径
* @return 返回Drawable资源
*/
public static Drawable fileToDrawable(String filepath) {
if (VMStr.isEmpty(filepath)) {
return null;
}
File file = new File(filepath);
if (file.exists()) {
Drawable drawable = Drawable.createFromPath(filepath);
return drawable;
}
return null;
}
@Override
public Drawable getDrawable(Context context, String skinName, int resId) {
String resName = context.getResources().getResourceEntryName(resId);
String resType = context.getResources().getResourceTypeName(resId);
if ("drawable".equalsIgnoreCase(resType) && "zip_background".equalsIgnoreCase(resName)) {
String dir = SkinFileUtils.getSkinDir(context);
File zipBackground = new File(dir, "zip_background.png");
if (zipBackground.exists()) {
return Drawable.createFromPath(zipBackground.getAbsolutePath());
}
return null;
}
return super.getDrawable(context, skinName, resId);
}
@Override
public Drawable getDrawable(Context context, String skinName, int resId) {
String resName = context.getResources().getResourceEntryName(resId);
String resType = context.getResources().getResourceTypeName(resId);
if ("drawable".equalsIgnoreCase(resType) && "zip_background".equalsIgnoreCase(resName)) {
String dir = SkinFileUtils.getSkinDir(context);
File zipBackground = new File(dir, "zip_background.png");
if (zipBackground.exists()) {
return Drawable.createFromPath(zipBackground.getAbsolutePath());
}
return null;
}
return super.getDrawable(context, skinName, resId);
}
@Override
public void handleMessage(Message msg) {
ImageView imageView = wrImageView.get();
if (imageView != null) {
Drawable d = Drawable.createFromPath(""+msg.obj);
imageView.setImageDrawable(d);
}
}
/**
* Get a drawable of the style at a particular index.
*
* @param context The current application context.
* @param value The index to obtain.
* @return A created Drawable, or null if
* something has gone horribly wrong.
*/
@Nullable
public Drawable getDrawable(Context context, int value) {
if (value < 0 || value >= getSize()) return null;
switch (type) {
case TYPE_VECTOR:
return VectorDrawableCompat.create(context.getResources(), resource[value], context.getTheme());
case TYPE_IMAGE:
return ContextCompat.getDrawable(context, resource[value]);
case TYPE_FILE:
String[] permissions = new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (!StaticUtils.isPermissionsGranted(context, permissions)) {
if (context instanceof Activity)
StaticUtils.requestPermissions((Activity) context, permissions);
return null;
} else {
try {
return Drawable.createFromPath(path[value]);
} catch (OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
default:
return null;
}
}
AppLovinNativeAdMapper(AppLovinNativeAd nativeAd, Context context) {
mNativeAd = nativeAd;
setHeadline(nativeAd.getTitle());
setBody(nativeAd.getDescriptionText());
setCallToAction(nativeAd.getCtaText());
ImageView mediaView = new ImageView(context);
ViewGroup.LayoutParams layoutParams =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mediaView.setLayoutParams(layoutParams);
ArrayList<NativeAd.Image> images = new ArrayList<>(1);
Uri imageUri = Uri.parse(nativeAd.getImageUrl());
Drawable imageDrawable = Drawable.createFromPath(imageUri.getPath());
Uri iconUri = Uri.parse(nativeAd.getIconUrl());
Drawable iconDrawable = Drawable.createFromPath(iconUri.getPath());
AppLovinNativeAdImage image = new AppLovinNativeAdImage(imageUri, imageDrawable);
AppLovinNativeAdImage icon = new AppLovinNativeAdImage(iconUri, iconDrawable);
images.add(image);
setImages(images);
setIcon(icon);
mediaView.setImageDrawable(imageDrawable);
setMediaView(mediaView);
setStarRating(nativeAd.getStarRating());
Bundle extraAssets = new Bundle();
extraAssets.putLong(AppLovinNativeAdapter.KEY_EXTRA_AD_ID, nativeAd.getAdId());
extraAssets.putString(AppLovinNativeAdapter.KEY_EXTRA_CAPTION_TEXT, nativeAd.getCaptionText());
setExtras(extraAssets);
setOverrideClickHandling(false);
setOverrideImpressionRecording(false);
}
@Override
protected Drawable doInBackground(Void... params) {
final String screenshotPath = label.getScreenshotPath();
LogUtils.v(TAG, "Spawning new LoadScreenshotTask(%d) for %s.", hashCode(), screenshotPath);
return Drawable.createFromPath(screenshotPath);
}
public void bind() {
if (background == null) {
shp = getContext().getSharedPreferences("wallpaper", Context.MODE_PRIVATE);
if (shp.getInt("wallpaper", 0) == ActorSDK.sharedActor().style.getDefaultBackgrouds().length) {
background = Drawable.createFromPath(BaseActorSettingsFragment.getWallpaperFile());
} else {
background = getResources().getDrawable(BackgroundPreviewView.getBackground(BackgroundPreviewView.getBackgroundIdByUri(messenger().getSelectedWallpaper(), getContext(), shp.getInt("wallpaper", 0))));
}
}
}
@Override
public synchronized Drawable get(Object key) {
if (!this.containsKey(key)) {
return null;
}
return Drawable.createFromPath(this.getPath(key.toString()));
}
private void setIcon(TextView view, String path) {
Drawable icon = Drawable.createFromPath(path);
Drawable checkbox = view.getCompoundDrawables()[2];
if (icon != null)
icon.setBounds(0, 0, checkbox.getIntrinsicWidth(), checkbox.getIntrinsicHeight());
view.setCompoundDrawables(icon, null, checkbox, null);
}
private void setLayoutBackgroundImage(String backgroundImagePath) {
Drawable image = Drawable.createFromPath(backgroundImagePath);
preview.setImageDrawable(image);
}
/**
* Create a drawable from file path name.
*/
public static Drawable createFromPath(String pathName) {
return Drawable.createFromPath(pathName);
}
public AppLovinUnifiedNativeAdMapper(Context context, AppLovinNativeAd nativeAd) {
mNativeAd = nativeAd;
setHeadline(mNativeAd.getTitle());
setBody(mNativeAd.getDescriptionText());
setCallToAction(mNativeAd.getCtaText());
final ImageView mediaView = new ImageView(context);
ViewGroup.LayoutParams layoutParams =
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mediaView.setLayoutParams(layoutParams);
ArrayList<NativeAd.Image> images = new ArrayList<>(1);
Uri imageUri = Uri.parse(mNativeAd.getImageUrl());
Drawable imageDrawable = Drawable.createFromPath(imageUri.getPath());
Uri iconUri = Uri.parse(mNativeAd.getIconUrl());
Drawable iconDrawable = Drawable.createFromPath(iconUri.getPath());
AppLovinNativeAdImage image = new AppLovinNativeAdImage(imageUri, imageDrawable);
AppLovinNativeAdImage icon = new AppLovinNativeAdImage(iconUri, iconDrawable);
images.add(image);
setImages(images);
setIcon(icon);
mediaView.setImageDrawable(imageDrawable);
setMediaView(mediaView);
int imageHeight = imageDrawable.getIntrinsicHeight();
if (imageHeight > 0) {
setMediaContentAspectRatio(imageDrawable.getIntrinsicWidth() / imageHeight);
}
setStarRating((double) mNativeAd.getStarRating());
Bundle extraAssets = new Bundle();
extraAssets.putLong(AppLovinNativeAdapter.KEY_EXTRA_AD_ID, mNativeAd.getAdId());
extraAssets.putString(AppLovinNativeAdapter.KEY_EXTRA_CAPTION_TEXT, mNativeAd.getCaptionText());
setExtras(extraAssets);
setOverrideClickHandling(false);
setOverrideImpressionRecording(false);
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//
// Loading arguments
//
try {
peer = Peer.fromBytes(getArguments().getByteArray("EXTRA_PEER"));
conversationVM = messenger().getConversationVM(peer);
} catch (IOException e) {
throw new RuntimeException(e);
}
//
// Display List
//
BindedDisplayList<Message> displayList = onCreateDisplayList();
if (isPrimaryMode) {
displayList.setLinearLayoutCallback(b -> {
if (layoutManager != null) {
layoutManager.setStackFromEnd(b);
}
});
}
//
// Main View
//
View res = inflate(inflater, container, R.layout.fragment_messages, displayList);
progressView = (CircularProgressBar) res.findViewById(R.id.loadingProgress);
progressView.setIndeterminate(true);
progressView.setVisibility(View.INVISIBLE);
//
// Loading background
//
Drawable background;
int[] backgrounds = ActorSDK.sharedActor().style.getDefaultBackgrouds();
String selectedWallpaper = messenger().getSelectedWallpaper();
if (selectedWallpaper != null) {
background = getResources().getDrawable(backgrounds[0]);
if (selectedWallpaper.startsWith("local:")) {
for (int i = 1; i < backgrounds.length; i++) {
if (getResources().getResourceEntryName(backgrounds[i]).equals(selectedWallpaper.replaceAll("local:", ""))) {
background = getResources().getDrawable(backgrounds[i]);
}
}
} else {
background = Drawable.createFromPath(BaseActorSettingsFragment.getWallpaperFile());
}
} else {
background = getResources().getDrawable(backgrounds[0]);
}
((ImageView) res.findViewById(R.id.chatBackgroundView)).setImageDrawable(background);
//
// List Padding
//
View footer = new View(getActivity());
footer.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Screen.dp(8)));
addHeaderView(footer); // Add Footer as Header because of reverse layout
View header = new View(getActivity());
header.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, Screen.dp(64)));
addFooterView(header); // Add Header as Footer because of reverse layout
//
// Init unread message index if available
//
recalculateUnreadMessageIfNeeded();
return res;
}
public static Drawable retrieveWallpaperDrawable() {
if (isExternalStorageReadable())
return Drawable.createFromPath(Environment.getExternalStorageDirectory() + "/TapUnlock/blurredWallpaper.png");
return null;
}