android 图片作为背景图片,Android设置网络图片为手机背景图片的方法
1 开通两个权限
2
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/white"
xmlns:android="http://schemas.android.com/apk/res/android"
>
android:id="@+id/myEdit"
android:layout_width="280px"
android:layout_height="wrap_content"
android:text="http://"
android:textSize="12sp"
android:layout_x="20px"
android:layout_y="42px"
>
android:id="@+id/myText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/str_title"
android:textSize="16sp"
android:textColor="@drawable/black"
android:layout_x="20px"
android:layout_y="12px"
>
android:id="@+id/myButton1"
android:layout_width="80px"
android:layout_height="45px"
android:text="@string/str_button1"
android:layout_x="70px"
android:layout_y="102px"
>
android:id="@+id/myButton2"
android:layout_width="80px"
android:layout_height="45px"
android:text="@string/str_button2"
android:layout_x="150px"
android:layout_y="102px"
>
android:id="@+id/myImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="20px"
android:layout_y="152px"
>
3
package dfzy.EX094;
/* import相关class */
import java.net.URL;
import java.net.URLConnection;
import java.net.URLConnection;
import dfzy.EX094.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class EX094 extends Activity
{
/* 变量声明 */
private Button mButton1;
private Button mButton2;
private EditText mEditText;
private ImageView mImageView;
private Bitmap bm;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* 初始化对象 */
mButton1 =(Button) findViewById(R.id.myButton1);
mButton2 =(Button) findViewById(R.id.myButton2);
mEditText = (EditText) findViewById(R.id.myEdit);
mImageView = (ImageView) findViewById(R.id.myImage);
mButton2.setEnabled(false);
/* 预览图片的Button */
mButton1.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
String path=mEditText.getText().toString();
if(path.equals(""))
{
showDialog("网址不可为空白!");
}
else
{
/* 传入type=1为预览图片 */
setImage(path,1);
}
}
});
/* 将图片设为桌面的Button */
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
try
{
String path=mEditText.getText().toString();
if(path.equals(""))
{
showDialog("网址不可为空白!");
}
else
{
/* 传入type=2为设置桌面 */
setImage(path,2);
}
}
catch (Exception e)
{
showDialog("读取错误!!");
bm = null;
mImageView.setImageBitmap(bm);
mButton2.setEnabled(false);
e.printStackTrace();
}
}
});
}
/* 将图片抓下来预览或并设置为桌面的方法 */
private void setImage(String path,int type)
{
try
{
URL url = new URL(path);
URLConnection conn = url.openConnection();
conn.connect();
if(type==1)
{
/* 预览图片 */
bm = BitmapFactory.decodeStream(conn.getInputStream());
mImageView.setImageBitmap(bm);
mButton2.setEnabled(true);
}
else if(type==2)
{
/* 设置为桌面 */
EX094.this.setWallpaper(conn.getInputStream());
bm = null;
mImageView.setImageBitmap(bm);
mButton2.setEnabled(false);
showDialog("桌面背景设置完成!");
}
}
catch (Exception e)
{
showDialog("读取错误!网址可能不是图片或网址错误!");
bm = null;
mImageView.setImageBitmap(bm);
mButton2.setEnabled(false);
e.printStackTrace();
}
}
/* 跳出Dialog的方法 */
private void showDialog(String mess){
new AlertDialog.Builder(EX094.this).setTitle("Message")
.setMessage(mess)
.setNegativeButton("确定", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
})
.show();
}
}4 效果是对的,不展示了
android 图片作为背景图片,Android设置网络图片为手机背景图片的方法相关推荐
- Android开发 调用系统相机相册图片功能,解决小米手机拍照或者图片横竖相反问题,及小米手机相册图片路径问题
Android开发 调用系统相机相册图片功能,解决小米手机拍照或者图片横竖相反问题,及小米手机相册图片路径问题 1.调用相机,兼容7.0 AndroidManifest配置 <providera ...
- IOS设置导航栏的背景图片和文字
IOS设置导航栏的背景图片和文字 - (void)viewDidLoad {[super viewDidLoad];[self.navigationBar setBackgroundImage:[UI ...
- html语言设置网页背景,HTML+CSS入门 设置网页中的背景图片的5个属性
本篇教程介绍了HTML+CSS入门 设置网页中的背景图片的5个属性,希望阅读本篇文章以后大家有所收获,帮助大家HTML+CSS入门. < 在css代码中设置背景图片的方法,包括背景图片.背景重复 ...
- 关于如何设置网页自动切换背景图片
这篇文章关于如何设置网页自动切换背景图片 如何设置背景图片 通过点击按钮来触发"切换背景图片"这个命令 ==重点== 如何在script里面改变网页的背景图片 设置背景图片自动切换 ...
- html添加背景图片并且填满div,css background-size与背景图片填满div(示例代码)
background-size与背景图片填满div 在开发中,常有需要将一张图片作为一个div的背景图片充满div的需求 background-size的取值及解释 background-size共有 ...
- html图片固定高宽比显示,科技常识:CSS背景图片固定宽高比自适应调整的实现方法...
今天小编跟大家讲解下有关CSS背景图片固定宽高比自适应调整的实现方法 ,相信小伙伴们对这个话题应该有所关注吧,小编也收集到了有关CSS背景图片固定宽高比自适应调整的实现方法 的相关资料,希望小伙伴们看 ...
- html背景图片在底部,CSS兑现固定DIV层背景图片且底部显示
CSS实现固定DIV层背景图片且底部显示 /*CSS缩写形式*/ div { background:url(/images/about_bg.jpg) no-repeat fixed; backgro ...
- android+单机button+设置背景图片,button设置点击更改背景图片
给button设置背景图片很简单,设置点击时的背景也简单,做一个selector选择器就好了.当我想在点击时和松开后背景不再改变回去时遇到了麻烦,无论怎么设置selector都不起作用.无奈上网求救, ...
- android的文本框内容居中显示图片,DIV+CSS中让布局居中_背景图片居中_文字内容居中...
在DIV CSS布局的页面里,从布局内容到页面里文章文字居中都是非常重要的,而 1.首先介绍使用css属性让整体布局的居中: 设置对象的父级内容居中,这里一个页面的为父级是什么呢?我们可以想象整个页面 ...
最新文章
- 一个预告|恭喜斯科特·阿伦森获得2021年ACM计算奖
- 数据科学真的是一份有前途的工作吗?
- 互斥锁在python中的应用
- Android JNI_OnLoad()函数
- 动态规划0—1背包问题
- Nature Communications | 白凡研究组揭示结直肠癌近端淋巴结与远端器官的转移路径...
- 【TensorFlow】TensorFlow从浅入深系列之三 -- 教你如何对MNIST手写识别
- 什么电脑的牌子好用_什么牌子的盘点机好用?
- 不能包含全角 正则_java常用正则表达式
- SQL Server中的STRING_SPLIT函数
- Memcached windows 下安装与应用
- RedHat 7配置bonding双网卡绑定
- HDOJ--1285--确定比赛名次
- 中国高性能计算服务器排名,2007中国高性能计算100强出炉
- 004 数字调制ASK,OOK,FSK,PSK,QAM,CPFSK
- 终于搞清前端和后端的区别啦!原来是这样的
- 未找到类型或命名空间名称 (是否缺少 using 指令或程序集引用?)
- Mysql复制表结构、表数据以及修改主键
- dirty cow的一点理解
- 点云处理:Kitti数据集中点云坐标转化到彩色摄像机坐标