- 博客(3)
- 资源 (5)
- 收藏
- 关注
![](https://csdnimg.cn/release/blogv2/dist/pc/img/listFixedTop.png)
原创 Android 进入应用市场评价应用
在Android的App中经常会见到从当前app跳转到一个应用商店且显示本软件详情的页面,让用户给自己的App进行评分的功能。下面简单介绍该功能的实现。要打开应用市场首先做的就是判断此手机是否安装了该应用市场 /** * 判断市场是否存在的方法 * @param context * @param packageName 应用市场包名 * @retur
2016-10-11 13:14:15
2826
1
ViewPagerFragment滑动切换
package com.pyy.viewpagerfragment;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.Display;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.pyy.viewpagerfragment.R;
public class MainActivity extends FragmentActivity {
public MainViewPager viewPager;
public List<Fragment> fragments = new ArrayList<Fragment>();
public String hello = "hello ";
private ImageView mTab1, mTab2, mTab3, mTab4;
private ImageView mTabImg;
private int currIndex = 0;// 当前页卡编号
private int zero = 0;// 动画图片偏移量
private int one;//单个水平动画位移
private int two;
private int three;
private FragmentManager fragmentManager;
private int displayWidth, displayHeight;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Display currDisplay = getWindowManager().getDefaultDisplay();//获取屏幕当前分辨率
displayWidth = currDisplay.getWidth();
displayHeight = currDisplay.getHeight();
one = displayWidth / 4; //设置水平动画平移大小
two = one * 2;
three = one * 3;
initViews();//初始化控件
fragments.add(new TabAFragment());
fragments.add(new TabBFragment());
fragments.add(new TabCFragment());
fragments.add(new TabDFragment());
fragmentManager = this.getSupportFragmentManager();
viewPager = (MainViewPager) findViewById(R.id.viewPager);
//viewPager.setSlipping(false);//设置ViewPager是否可以滑动
viewPager.setOffscreenPageLimit(4);
viewPager.setOnPageChangeListener(new MyOnPageChangeListener());
viewPager.setAdapter(new MyPagerAdapter());
}
/**
*@Title: initViews
*@Description: TODO初始化控件
*/
public void initViews() {
final LinearLayout tab1Layout = (LinearLayout) findViewById(R.id.tab1Layout);
mTab1 = (ImageView) findViewById(R.id.img_weixin);
mTab2 = (ImageView) findViewById(R.id.img_address);
mTab3 = (ImageView) findViewById(R.id.img_friends);
mTab4 = (ImageView) findViewById(R.id.img_settings);
mTabImg = (ImageView) findViewById(R.id.img_tab_now);
mTab1.setOnClickListener(new MyOnClickListener(0));
mTab2.setOnClickListener(new MyOnClickListener(1));
mTab3.setOnClickListener(new MyOnClickListener(2));
mTab4.setOnClickListener(new MyOnClickListener(3));
//以下是设置移动条的初始位置
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) mTabImg
.getLayoutParams();
int[] location = new int[2];
mTab1.getLocationInWindow(location);
int marginLeft = (displayWidth / 4 - 122) / 2;//122是tab下面移动条的宽度
lp.setMargins(marginLeft, 0, 0, 0);
mTabImg.setLayoutParams(lp);
mTabImg.setVisibility(View.VISIBLE);
}
/**
* @ClassName: MyOnPageChangeListener
* @Description: TODO页卡切换监听
* @author Panyy
* @date 2013 2013年11月6日 下午2:08:10
*
*/
public class MyOnPageChangeListener implements OnPageChangeListener {
@Override
public void onPageSelected(int arg0) {
Animation animation = null;
switch (arg0) {
case 0:
mTab1.setImageDrawable(getResources().getDrawable(
R.drawable.tab_weixin_pressed));
if (currIndex == 1) {
animation = new TranslateAnimation(one, 0, 0, 0);
mTab2.setImageDrawable(getResources().getDrawable(
R.drawable.tab_address_normal));
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, 0, 0, 0);
mTab3.setImageDrawable(getResources().getDrawable(
R.drawable.tab_find_frd_normal));
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, 0, 0, 0);
mTab4.setImageDrawable(getResources().getDrawable(
R.drawable.tab_settings_normal));
}
break;
case 1:
mTab2.setImageDrawable(getResources().getDrawable(
R.drawable.tab_address_pressed));
if (currIndex == 0) {
animation = new TranslateAnimation(zero, one, 0, 0);
mTab1.setImageDrawable(getResources().getDrawable(
R.drawable.tab_weixin_normal));
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, one, 0, 0);
mTab3.setImageDrawable(getResources().getDrawable(
R.drawable.tab_find_frd_normal));
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, one, 0, 0);
mTab4.setImageDrawable(getResources().getDrawable(
R.drawable.tab_settings_normal));
}
break;
case 2:
mTab3.setImageDrawable(getResources().getDrawable(
R.drawable.tab_find_frd_pressed));
if (currIndex == 0) {
animation = new TranslateAnimation(zero, two, 0, 0);
mTab1.setImageDrawable(getResources().getDrawable(
R.drawable.tab_weixin_normal));
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, two, 0, 0);
mTab2.setImageDrawable(getResources().getDrawable(
R.drawable.tab_address_normal));
} else if (currIndex == 3) {
animation = new TranslateAnimation(three, two, 0, 0);
mTab4.setImageDrawable(getResources().getDrawable(
R.drawable.tab_settings_normal));
}
break;
case 3:
mTab4.setImageDrawable(getResources().getDrawable(
R.drawable.tab_settings_pressed));
if (currIndex == 0) {
animation = new TranslateAnimation(zero, three, 0, 0);
mTab1.setImageDrawable(getResources().getDrawable(
R.drawable.tab_weixin_normal));
} else if (currIndex == 1) {
animation = new TranslateAnimation(one, three, 0, 0);
mTab2.setImageDrawable(getResources().getDrawable(
R.drawable.tab_address_normal));
} else if (currIndex == 2) {
animation = new TranslateAnimation(two, three, 0, 0);
mTab3.setImageDrawable(getResources().getDrawable(
R.drawable.tab_find_frd_normal));
}
break;
}
currIndex = arg0;
animation.setFillAfter(true);// True:图片停在动画结束位置
animation.setDuration(150);
mTabImg.startAnimation(animation);
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
}
/**
* @ClassName: MyOnClickListener
* @Description: TODO头标点击监听
* @author Panyy
* @date 2013 2013年11月6日 下午2:46:08
*
*/
private class MyOnClickListener implements View.OnClickListener {
private int index = 0;
public MyOnClickListener(int i) {
index = i;
}
@Override
public void onClick(View v) {
viewPager.setCurrentItem(index);
}
};
/**
* @ClassName: MyPagerAdapter
* @Description: TODO填充ViewPager的数据适配器
* @author Panyy
* @date 2013 2013年11月6日 下午2:37:47
*
*/
private class MyPagerAdapter extends PagerAdapter {
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
@Override
public int getCount() {
return fragments.size();
}
@Override
public void destroyItem(View container, int position, Object object) {
((ViewPager) container).removeView(fragments.get(position)
.getView());
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = fragments.get(position);
if (!fragment.isAdded()) { // 如果fragment还没有added
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.add(fragment, fragment.getClass().getSimpleName());
ft.commit();
/**
* 在用FragmentTransaction.commit()方法提交FragmentTransaction对象后
* 会在进程的主线程中,用异步的方式来执行。
* 如果想要立即执行这个等待中的操作,就要调用这个方法(只能在主线程中调用)。
* 要注意的是,所有的回调和相关的行为都会在这个调用中被执行完成,因此要仔细确认这个方法的调用位置。
*/
fragmentManager.executePendingTransactions();
}
if (fragment.getView().getParent() == null) {
container.addView(fragment.getView()); // 为viewpager增加布局
}
return fragment.getView();
}
};
}
2015-07-01
Android相机调用
package com.example.camera;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Locale;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
@SuppressLint("SdCardPath")
public class MainActivity extends Activity {
/** Called when the activity is first created. */
private Button button;
private ImageView view;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button1);
view= (ImageView)findViewById(R.id.imageView1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
});
}
@SuppressLint("SdCardPath")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
String sdStatus = Environment.getExternalStorageState();
if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
Log.i("TestFile",
"SD card is not avaiable/writeable right now.");
return;
}
new DateFormat();
String name = DateFormat.format("yyyyMMdd_hhmmss",Calendar.getInstance(Locale.CHINA)) + ".jpg";
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
Bundle bundle = data.getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式
FileOutputStream b = null;
File file = new File("/sdcard/Image/");
file.mkdirs();// 创建文件夹
String fileName = "/sdcard/Image/"+name;
try {
b = new FileOutputStream(fileName);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
b.flush();
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try
{
view.setImageBitmap(bitmap);// 将图片显示在ImageView里
}catch(Exception e)
{
Log.e("error", e.getMessage());
}
}
}
}
2015-07-01
Android二维码扫描
package com.hsy.Zxing.Activity;
import java.io.IOException;
import java.util.Vector;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Vibrator;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.Result;
import com.hsy.Zxing.Gan.camera.CameraManager;
import com.hsy.Zxing.Gan.decoding.CaptureActivityHandler;
import com.hsy.Zxing.Gan.decoding.InactivityTimer;
import com.hsy.Zxing.Gan.view.ViewfinderView;
import com.zijunlin.Zxing.Demo.R;
public class CaptureActivity extends Activity implements Callback
{
private CaptureActivityHandler handler;
private ViewfinderView viewfinderView;
private boolean hasSurface;
private Vector<BarcodeFormat> decodeFormats;
private String characterSet;
private InactivityTimer inactivityTimer;
private MediaPlayer mediaPlayer;
private boolean playBeep;
private static final float BEEP_VOLUME = 0.10f;
private boolean vibrate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 初始化 CameraManager
CameraManager.init(getApplication());
viewfinderView = (ViewfinderView) findViewById(R.id.viewfinder_view);
hasSurface = false;
inactivityTimer = new InactivityTimer(this);
}
@Override
protected void onResume()
{
super.onResume();
SurfaceView surfaceView = (SurfaceView) findViewById(R.id.preview_view);
SurfaceHolder surfaceHolder = surfaceView.getHolder();
if (hasSurface)
{
initCamera(surfaceHolder);
}
else
{
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
decodeFormats = null;
characterSet = null;
playBeep = true;
AudioManager audioService = (AudioManager) getSystemService(AUDIO_SERVICE);
if (audioService.getRingerMode() != AudioManager.RINGER_MODE_NORMAL)
{
playBeep = false;
}
initBeepSound();
vibrate = true;
}
@Override
protected void onPause()
{
super.onPause();
if (handler != null)
{
handler.quitSynchronously();
handler = null;
}
CameraManager.get().closeDriver();
}
@Override
protected void onDestroy()
{
inactivityTimer.shutdown();
super.onDestroy();
}
private void initCamera(SurfaceHolder surfaceHolder)
{
try
{
CameraManager.get().openDriver(surfaceHolder);
}
catch (IOException ioe)
{
return;
}
catch (RuntimeException e)
{
return;
}
if (handler == null)
{
handler = new CaptureActivityHandler(this, decodeFormats, characterSet);
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
}
@Override
public void surfaceCreated(SurfaceHolder holder)
{
if (!hasSurface)
{
hasSurface = true;
initCamera(holder);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
hasSurface = false;
}
public ViewfinderView getViewfinderView()
{
return viewfinderView;
}
public Handler getHandler()
{
return handler;
}
public void drawViewfinder()
{
viewfinderView.drawViewfinder();
}
public void handleDecode(final Result obj, Bitmap barcode)
{
inactivityTimer.onActivity();
playBeepSoundAndVibrate();
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
if (barcode == null)
{
dialog.setIcon(null);
}
else
{
Drawable drawable = new BitmapDrawable(barcode);
dialog.setIcon(drawable);
}
dialog.setTitle("扫描结果");
dialog.setMessage(obj.getText());
dialog.setNegativeButton("确定", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
//用默认浏览器打开扫描得到的地址
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
Uri content_url = Uri.parse(obj.getText());
intent.setData(content_url);
startActivity(intent);
finish();
}
});
dialog.setPositiveButton("取消", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
finish();
}
});
dialog.create().show();
}
private void initBeepSound()
{
if (playBeep && mediaPlayer == null)
{
// The volume on STREAM_SYSTEM is not adjustable, and users found it
// too loud,
// so we now play on the music stream.
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setOnCompletionListener(beepListener);
AssetFileDescriptor file = getResources().openRawResourceFd(R.raw.beep);
try
{
mediaPlayer.setDataSource(file.getFileDescriptor(), file.getStartOffset(), file.getLength());
file.close();
mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
mediaPlayer.prepare();
}
catch (IOException e)
{
mediaPlayer = null;
}
}
}
private static final long VIBRATE_DURATION = 200L;
private void playBeepSoundAndVibrate()
{
if (playBeep && mediaPlayer != null)
{
mediaPlayer.start();
}
if (vibrate)
{
Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
vibrator.vibrate(VIBRATE_DURATION);
}
}
/**
* When the beep has finished playing, rewind to queue up another one.
*/
private final OnCompletionListener beepListener = new OnCompletionListener()
{
public void onCompletion(MediaPlayer mediaPlayer)
{
mediaPlayer.seekTo(0);
}
};
}
2015-06-29
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人