我尝试将图像保存到WathsappIMG但是当我去图像库android我没有看到图像和那里的图像进入目录可以从ES文件资源管理器中看到
OutputStream output;
// Find the SD Card path
File filepath = Environment.getExternalStorageDirectory();
// Create a new folder in SD Card
File dir = new File(filepath.getAbsolutePath()
+ "/WhatSappIMG/");
dir.mkdirs();
// Retrieve the image from the res folder
BitmapDrawable drawable = (BitmapDrawable) principal.getDrawable();
Bitmap bitmap1 = drawable.getBitmap();
// Create a name for the saved image
File file = new File(dir, "Wallpaper.jpg" );
try {
output = new FileOutputStream(file);
// Compress into png format image from 0% - 100%
bitmap1.compress(Bitmap.CompressFormat.JPEG, 100, output); …Run Code Online (Sandbox Code Playgroud) 我试图在我的Android应用程序的图像上应用效果(棕褐色,亮度,绽放和其他图像效果,如果它们的API可用).但是我完全无法获得用于解决此类问题的精确且良好的代码或概念.虽然Android 4.0(API 14)已经内置了android.media.effect api,但我在Android 2.1中工作,它只有Bitmap,Drawable,DrawableBitmap等,但我没有得到它.
我得到imageView的宽度0.下面是代码.
xml文件:
<ImageView
android:id="@+id/img"
android:layout_width="200dp"
android:layout_height="200dp" />
Run Code Online (Sandbox Code Playgroud)
活动:
@Override
protected void onResume() {
super.onResume();
ImageView img = (ImageView) findViewById(R.id.img);
Log.d(TAG, "width : " + img.getWidth());
}
Run Code Online (Sandbox Code Playgroud)
我不想使用下面的代码,因为我需要将这些代码放在很多地方.
ViewTreeObserver vto = img.getViewTreeObserver();
vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
ImageView img = (ImageView) findViewById(R.id.img);
Log.d(TAG, "width : " + img.getWidth());
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
我的解决方法: 我实际上后来不需要在活动中添加此代码.我将它添加到非活动类而不是ImageView的宽度我使用了Bitmap图像的宽度,现在没有这个问题.
目前我正在Android应用程序中绘制PNG图像,如下所示:
ImageView image = new ImageView(context);
image.setImageDrawable(context.getResources().getDrawable(R.drawable.testimage))
Run Code Online (Sandbox Code Playgroud)
如果我在数据库中有一个图像名称列表,有没有办法使用图像名称设置上面的drawable?我已经有了通过数据库的代码,我只是想根据从这里取得的值来绘制图像.
例如,DB的记录:
ID: Name: ImageName:
- Test testimage
Run Code Online (Sandbox Code Playgroud)
因此,当我正在阅读此记录时,我有一个值为"testimage"的字符串,然后我想将图像设置为drawable R.drawable.testimage.
我想做的一种方式是这样的:
int image = R.drawable.blank; // blank image
// testimage.png is the image name from the database
if(imageName.toString().equals("testimage.png"))
image = R.drawable.testimage;
else if(imageName.toString().equals("another.png"))
image = R.drawable.another;
else if(imageName.toString().equals("etc.png"))
image = R.drawable.etc;
Run Code Online (Sandbox Code Playgroud)
但是这不是很有效!
谢谢
我是一个有windows背景的程序员,我是Java和android的新手,
我想创建一个显示图表的小部件(不是应用程序).
经过长时间的研究,我知道我可以用Canvas,imageviews和Bitmaps做到这一点.
我绘制的画布应该与小部件大小相同.
所以问题是:我如何知道小部件大小(或图像视图大小),以便我可以将它提供给该功能
Bitmap.createBitmap(width_xx,height_yy,Config.ARGB_8888);
代码片段:在计时器运行方法中:
@Override
public void run() {
Bitmap bitmap = Bitmap.createBitmap(??, ??, Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
//create new paint
Paint p = new Paint();
p.setAntiAlias(true);
p.setStrokeWidth(1);
//draw circle
//here i can use the width and height to scale the circle
canvas.drawCircle(50, 50, 7, p);
remoteViews.setImageViewBitmap(R.id.imageView, bitmap);
Run Code Online (Sandbox Code Playgroud) 我需要在ImageView的边缘创建一个alpha渐变.最好只使用XML.
android gradient alpha-transparency imageview android-imageview
我已经使用了ThreePhaseBottomLibrary,根据我的经验,当我向上滚动时动画应该按照上面的图像并行!
下面是我的Fragment类.它的工作正常,除了这个图像并行动画按照屏幕:
Myfragment.java
public class MyFragment extends BottomSheetFragment {
private BottomSheetLayout mBottomSheetLayout;
private ImageView mBottomSheetBackgroundImageView;
private int mBottomSheetHeight;
private ImageView movingIconImageView;
private AppBarLayout mAppBarLayout;
private int mMStartMarginBottom;
private int mMStartMarginLeft;
private Toolbar mToolbar;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_my, container, false);
mBottomSheetHeight = getResources().getDimensionPixelSize(R.dimen.header_height);
mAppBarLayout = (AppBarLayout) view.findViewById(R.id.appbar);
view.setMinimumHeight(getResources().getDisplayMetrics().heightPixels);
CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar);
//collapsingToolbar.setTitle("Title");
collapsingToolbar.setTitleEnabled(false);
mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
//final AppCompatActivity activity = (AppCompatActivity) getActivity();
//activity.setSupportActionBar(toolbar); …Run Code Online (Sandbox Code Playgroud) 我想显示图像(包括滚动和缩放功能).另外,我需要动态地为图像添加一些叠加层.叠加层与图像中的内容有关系.
换句话说:我想用一些标记来实现Map,但我提供了自己的地图材质.
我的主要问题是:我怎么能这样做?
到目前为止我尝试过的
问题
我的问题有一个简单的解决方案或方法吗?
在Android中运行时添加和移动视图的正确方法是什么?(通常,我会使用基本布局并使用保证金 - 这是最佳做法吗?)
请有人指出我正确的方向.
闵.API等级:10
的src应拉伸其宽度match_parent,同时保持高宽比.当图像大于父图像时,它会正确缩小.但是当图像较小时,它不会向上扩展.(插图显示了所需的行为).

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:src="@drawable/foo"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
使用ScaleType.fitXY伸展width唯一
我正在尝试将照片设置为背景Activity.我希望背景是一个全屏图像(没有边框).
因为我希望图像在没有拉伸/挤压的情况下填充整个活动的背景(即X和Y的不成比例的缩放)并且我不介意是否必须裁剪照片,我正在使用RelativeLayout带有ImageView(带android:scaleType="centerCrop")和我的布局的其余部分由a ScrollView和它的孩子组成.
<!-- Tried this with a FrameLayout as well... -->
<RelativeLayout>
<!-- Background -->
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"/>
<!-- Form -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView>
<LinearLayout>
...
<EditText/>
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
问题是布局的其余部分有一些EditText视图,当软键盘出现时,ImageView会重新调整大小.我希望背景保持不变,无论软键盘是否可见.
我已经看到很多关于ImageViews被重新调整大小的问题但是(imo)没有令人满意的答案.其中大多数只是设置android:windowSoftInputMode="adjustPan"- 这并不总是实用的,特别是如果您希望用户能够滚动活动 - 或使用getWindow().setBackgroundDrawable()不裁剪图像.
我已经设法将ImageView子类化并覆盖它onMeasure()(请参阅我的答案:当键盘打开时ImageView调整大小)以便它可以强制固定的高度和宽度 - 等于设备的屏幕尺寸 - 根据标志但我是想知道是否有更好的方法来实现我想要的结果.
总而言之,我的问题是:如何将活动的背景设置为全屏照片
使用scale type ="centerCrop",使照片均匀缩放(保持其纵横比),因此两个尺寸(宽度和高度)将等于或大于视图的相应尺寸;
弹出软键盘时不会调整大小;
回答:
我最后关注了@pskink的建议和子类BitmapDrawable(见下面的答案).我不得不做一些调整,以确保BackgroundBitmapDrawable …
java android android-layout android-imageview background-drawable