我有一个带有图片库的应用程序,我希望用户可以将其保存到自己的图库中.我已经创建了一个带有单个声音"保存"的选项菜单,但问题是......我怎样才能将图像保存到图库中?
这是我的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.menuFinale:
imgView.setDrawingCacheEnabled(true);
Bitmap bitmap = imgView.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
我不确定这部分代码:
File root = Environment.getExternalStorageDirectory();
File file = new File(root.getAbsolutePath()+"/DCIM/Camera/img.jpg");
Run Code Online (Sandbox Code Playgroud)
保存到画廊是否正确?不幸的是代码不起作用:(
我在我的Android应用程序中有一个imageview,我正在使用像给出onClick事件的按钮,但正如你可能猜到的那样,点击后它不会给imageview一个可点击的效果.我怎样才能做到这一点?
嗨我想要一个图像的进度条,它将在图像加载时显示,但是当图像加载完成时我想将它设置为消失.早些时候,我正在使用Picasso库.但我不知道如何将它与Glide库一起使用.我知道有一些资源就绪功能,但我不知道如何使用它.谁能帮我?
毕加索图书馆代码
Picasso.with(mcontext).load(imgLinkArray.get(position).mUrlLink)
.into(imageView, new Callback() {
@Override
public void onSuccess() {
progressBar.setVisibility(View.GONE);
}
@Override
public void onError() {
}
})
;
Run Code Online (Sandbox Code Playgroud)
现在我如何用Glide做到这一点?
Glide.with(mcontext).load(imgLinkArray.get(position).mUrlLink)
.into(imageView);
Run Code Online (Sandbox Code Playgroud)
我可以通过Glide加载图像,但是progressBar.setVisibility(View.GONE);如果图像被加载,我怎么能在代码中写一些东西呢?
经常被问到,从来没有回答过(至少不是以可重复的方式).
我有一个图像视图,其图像小于视图.我想将图像缩放到屏幕的宽度,并调整ImageView的高度以反映图像的比例正确高度.
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
Run Code Online (Sandbox Code Playgroud)
这导致图像以其原始尺寸(小于屏幕宽度)为中心,边缘为边缘.不好.
所以我补充道
android:adjustViewBounds="true"
Run Code Online (Sandbox Code Playgroud)
同样的效果,没有好处.我补充道
android:scaleType="centerInside"
Run Code Online (Sandbox Code Playgroud)
同样的效果,没有好处.我换centerInside了fitCenter.同样的效果,没有好处.我换centerInside了centerCrop.
android:scaleType="centerCrop"
Run Code Online (Sandbox Code Playgroud)
现在,最后,图像缩放到屏幕的宽度 - 但在顶部和底部裁剪!所以我改变了centerCrop对fitXY.
android:scaleType="fitXY"
Run Code Online (Sandbox Code Playgroud)
现在,图像缩放到屏幕的宽度,但没有在y轴上缩放,导致图像失真.
删除android:adjustViewBounds="true"无效.android:layout_gravity如其他地方所建议的,添加一个也没有效果.
我尝试过其他组合 - 无济于事.所以,请有人知道:
如何设置ImageView的XML来填充屏幕的宽度,缩放较小的图像以填充整个视图,显示具有宽高比的图像而不会失真或裁剪?
编辑:我也尝试设置任意数字高度.这仅对centerCrop设置有影响.它会根据视图高度垂直扭曲图像.
我在LinearLayout中有一些ImageView.我需要缩小ImageViews,以便它们保持纵横比,同时垂直安装在LinearLayout内部.水平地,我只需要它们彼此相邻.
我为此制作了一个简化的测试床,它嵌套了加权的布局,这样我就可以为ImageViews提供一个宽但不高的LinearLayout -
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<LinearLayout android:id="@+id/linearLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="5">
<LinearLayout android:id="@+id/linearLayout3" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1">
<ImageView android:id="@+id/imageView1" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/test_image" android:scaleType="fitStart"></ImageView>
<ImageView android:id="@+id/imageView2" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/test_image" android:scaleType="fitStart"></ImageView>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout4" android:layout_height="fill_parent" android:layout_width="fill_parent" android:layout_weight="1"></LinearLayout>
</LinearLayout>
<LinearLayout android:id="@+id/linearLayout2" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1"></LinearLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
(在Eclipse布局编辑器中,图像被垂直剪切而根本没有缩放 - 但这只是我们学会爱的那些特性之一)
在硬件上运行时,图像会缩放到正确的高度,同时保留其纵横比.我的问题是他们不是彼此相邻的.每个ImageView的高度正确匹配LinearLayout的高度.每个ImageView的宽度是未缩放图像的宽度 - 实际按比例缩小的图像出现在其ImageView的左侧.因此,我在图像之间会有很大的差距.
理想情况下,我想通过XML来管理这个,如果这是不可能的,我理解使用自定义视图可能是最好的解决方案.
我尝试创建一个IconView(扩展ImageView)类来覆盖onMeasure,这样我就可以通过根据高度缩放宽度来创建所需大小的图像视图.但是传递给函数的parentWidth和parentHeight是屏幕的尺寸而不是LinearLayout容器的尺寸.
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int parentWidth = MeasureSpec.getSize(widthMeasureSpec);
int parentHeight = MeasureSpec.getSize(heightMeasureSpec);
// Calculations followed by calls to setMeasuredDimension, setLayoutParams
super.onMeasure(widthMeasureSpec, heightMeasureSpec); …Run Code Online (Sandbox Code Playgroud) 我需要以编程方式将imageview的高度设置为matchparent.如果它是一个固定的高度,我知道如何设置.
但我怎么能把它设置为matchparent?
编辑:
实际上父布局的高度是动态的.所以我需要将imageview的高度设为父级的高度.
我想设置LayoutParams一个,ImageView但似乎无法找到正确的方法来做到这一点.
我只能在API中找到各种文档ViewGroups,但不是ImageView.但ImageView似乎有这个功能.
这段代码不起作用......
myImageView.setLayoutParams(new ImageView.LayoutParams(30,30));
Run Code Online (Sandbox Code Playgroud)
我该怎么做?
所以我有一个ImageView设置
android:maxHeight="100px"
android:maxWidth="250px"
android:minHeight="100px"
android:minWidth="250px"
android:scaleType="centerInside"
Run Code Online (Sandbox Code Playgroud)
此图像视图用于显示从图库或相机获取的图片.在这两种情况下,图像都不会调整大小以适应图像视图,它只是根据需要拉伸其空间.
知道怎么让它留在那些界限内吗?
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/txtDescription"
android:layout_below="@+id/txtSubject"
android:inputType="textMultiLine"
android:height="80px"
android:hint="@string/description"></EditText>
<EditText
android:layout_height="wrap_content"
android:layout_below="@+id/txtDescription"
android:layout_width="fill_parent"
android:id="@+id/txtMorada"
android:hint="@string/address" />
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/txtMorada"
android:id="@+id/btGPS"
android:layout_alignParentLeft="true"
android:src="@drawable/ic_menu_compass"></ImageButton>
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="@+id/btGPS"
android:layout_marginTop="25px"
android:id="@+id/imgPoint"
android:src="@drawable/google_logo_small"
android:maxHeight="100px"
android:maxWidth="250px"
android:minHeight="100px"
android:minWidth="250px"
android:scaleType="centerInside"></ImageView>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_toRightOf="@+id/imgPoint"
android:id="@+id/btGallery"
android:layout_below="@+id/btCamera"
android:src="@drawable/ic_menu_gallery"
android:layout_alignParentRight="true"></ImageButton>
<Button
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="10px"
android:id="@+id/btSubmit"
android:layout_below="@+id/btGallery"
android:text="@string/submit"></Button>
<ImageButton
android:layout_height="wrap_content"
android:id="@+id/btMap"
android:layout_below="@+id/txtMorada"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:src="@drawable/ic_menu_mapmode"></ImageButton>
<TextView
android:layout_below="@+id/txtMorada"
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/btMap"
android:layout_height="wrap_content"
android:id="@+id/lblNewPointLatitude"
android:text="Latitude"></TextView>
<TextView
android:layout_width="wrap_content"
android:layout_toLeftOf="@+id/btMap"
android:layout_height="wrap_content"
android:id="@+id/lblNewPointLongitude"
android:layout_below="@+id/lblNewPointLatitude" …Run Code Online (Sandbox Code Playgroud) 我有一个带有ImageViews的GridView.我每行有3个.我可以使用WRAP_CONTENT和scaleType = CENTER_CROP正确设置宽度,但我不知道如何将ImageView的大小设置为正方形.这是我到目前为止所做的,除了高度,它似乎没问题,那就是"静态":
imageView = new ImageView(context);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(GridView.LayoutParams.WRAP_CONTENT, 300));
Run Code Online (Sandbox Code Playgroud)
我在适配器里面做的.
我对使用src或背景有点困惑ImageView.我知道前者意味着这个的内容ImageView而后者意味着背景ImageView.但是如何检测使用哪一个?我没有看到差异.
android ×10
imageview ×10
layout ×2
background ×1
button ×1
custom-view ×1
dynamic ×1
formatting ×1
gallery ×1
height ×1
java ×1
resize ×1
save-image ×1
size ×1
src ×1
stretch ×1