Java tutorial
/******************************************************************************* * Copyright 2011, 2012 Chris Banes. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. *******************************************************************************/ package news.pangbo.com.pangbbonews.ui.common; import android.graphics.Bitmap; import android.graphics.Color; import android.os.Bundle; import android.os.Environment; import android.support.annotation.NonNull; import android.support.v4.view.PagerAdapter; import android.support.v4.view.ViewPager; import android.view.View; import android.view.ViewGroup; import android.view.ViewGroup.LayoutParams; import com.lidroid.xutils.util.LogUtils; import com.nostra13.universalimageloader.core.assist.FailReason; import com.nostra13.universalimageloader.core.listener.ImageLoadingListener; import com.pangbo.android.thirdviews.HackyViewPager; import com.pangbo.android.thirdviews.photoview.PhotoView; import com.pangbo.android.utils.ToastUtil; import com.r0adkll.slidr.Slidr; import com.r0adkll.slidr.model.SlidrConfig; import com.r0adkll.slidr.model.SlidrPosition; import java.io.File; import java.io.FileOutputStream; import java.util.ArrayList; import java.util.List; import news.pangbo.com.pangbbonews.R; import news.pangbo.com.pangbbonews.base.BaseActivity; /** * Lock/Unlock button is added to the ActionBar. * Use it to temporarily disable ViewPager navigation in order to correctly interact with ImageView by gestures. * Lock/Unlock state of ViewPager is saved and restored on configuration changes. * * Julia Zudikova */ public class ShowPicViewPagerActivity extends BaseActivity { private static final String ISLOCKED_ARG = "isLocked"; private ViewPager mViewPager; private SamplePagerAdapter adapter = null; //? private void initSlidr() { SlidrConfig config = new SlidrConfig.Builder().position(SlidrPosition.LEFT).sensitivity(1f) .scrimColor(Color.BLACK).scrimStartAlpha(0.8f).scrimEndAlpha(0f).velocityThreshold(2400) .distanceThreshold(0.25f).edge(true).edgeSize(0.18f).build(); Slidr.attach(this, config); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_pager); initSlidr(); mViewPager = (HackyViewPager) findViewById(R.id.view_pager); adapter = new SamplePagerAdapter(); if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("urls")) { adapter.urls = getIntent().getExtras().getStringArrayList("urls"); } ; assert adapter.urls != null; final int size = adapter.urls.size(); initActionBar((getIntent().getExtras().getInt("position") + 1) + "/" + size, R.mipmap.icon_back, "?", listener); mViewPager.setAdapter(adapter); mViewPager.setCurrentItem(getIntent().getExtras().getInt("position")); mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { initActionBar((position + 1) + "/" + size, R.mipmap.icon_back, "?", listener); } @Override public void onPageScrollStateChanged(int state) { } }); if (savedInstanceState != null) { boolean isLocked = savedInstanceState.getBoolean(ISLOCKED_ARG, false); ((HackyViewPager) mViewPager).setLocked(isLocked); } } private View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { if (v.getId() == R.id.iv_left) { finish(); } if (v.getId() == R.id.tv_right) { LogUtils.i("-->click tv_right"); savaImage(adapter.urls.get(mViewPager.getCurrentItem())); } } }; static class SamplePagerAdapter extends PagerAdapter { // private static final int[] sDrawables = { R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper, // R.drawable.wallpaper, R.drawable.wallpaper, R.drawable.wallpaper }; public void setUrls(List<String> urls) { this.urls = urls; } public List<String> urls = new ArrayList<>(); @Override public int getCount() { return urls.size(); } @Override public View instantiateItem(ViewGroup container, int position) { PhotoView photoView = new PhotoView(container.getContext()); // photoView.setImageResource(sDrawables[position]); com.nostra13.universalimageloader.core.ImageLoader.getInstance().displayImage(urls.get(position), photoView); // Now just add PhotoView to ViewPager and return it container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); return photoView; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } } // private void savaPic(Uri downloaded) { // if(downloaded == null) { // ToastUtil.showToast(ShowPicViewPagerActivity.this,"????"); // return; // } // // File path = Environment.getExternalStorageDirectory(); // File dir = new File(path, "QBang/Image"); // if(!dir.exists()) // dir.mkdirs(); // // File from = new File(downloaded.getPath()); // String name = from.getName() + ".jpg"; // File to = new File(dir.getAbsolutePath(), name); // if(to.exists()) { // ToastUtil.showToast(ShowPicViewPagerActivity.this, "???"); // } // copyFile(from.getAbsolutePath(), to.getAbsolutePath()); // } // public void copyFile(String oldPath, String newPath) { // try { // int bytesum = 0; // int byteread = 0; // File oldfile = new File(oldPath); // if (oldfile.exists()) { // InputStream inStream = new FileInputStream(oldPath); // FileOutputStream fs = new FileOutputStream(newPath); // byte[] buffer = new byte[1444]; // while ( (byteread = inStream.read(buffer)) != -1) { // bytesum += byteread; // System.out.println(bytesum); // fs.write(buffer, 0, byteread); // } // inStream.close(); // } // } // catch (Exception e) { // Toast.makeText(this, "??", Toast.LENGTH_SHORT).show(); // e.printStackTrace(); // } finally { // Toast.makeText(this, "???"+newPath, Toast.LENGTH_SHORT).show(); // } // } private void savaImage(String imgUrl) { com.nostra13.universalimageloader.core.ImageLoader.getInstance().loadImage(imgUrl, new ImageLoadingListener() { @Override public void onLoadingStarted(String imageUri, View view) { } @Override public void onLoadingFailed(String imageUri, View view, FailReason failReason) { } @Override public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { boolean isSDExist = Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); if (isSDExist) { saveImg(loadedImage); } else { ToastUtil.showToast(getApplicationContext(), "SD????"); } } @Override public void onLoadingCancelled(String imageUri, View view) { } }); } public void saveImg(Bitmap photo) { String path_file_img = Environment.getExternalStorageDirectory().getPath() + "/Qbang/down/" + System.currentTimeMillis() + ".jpg"; File file_img = new File(path_file_img); File dir = new File(Environment.getExternalStorageDirectory().getPath() + "/Qbang/down/"); if (!dir.exists()) { dir.mkdirs(); } if (file_img.exists()) { file_img.delete(); } FileOutputStream fos = null; try { file_img.createNewFile(); fos = new FileOutputStream(file_img); photo.compress(Bitmap.CompressFormat.JPEG, 100, fos); } catch (Exception e) { e.getStackTrace(); } finally { if (fos != null) { try { fos.flush(); fos.close(); ToastUtil.showToast(getApplicationContext(), "??" + path_file_img); } catch (Exception e) { e.printStackTrace(); } } } } private boolean isViewPagerActive() { return (mViewPager != null && mViewPager instanceof HackyViewPager); } @Override protected void onSaveInstanceState(@NonNull Bundle outState) { if (isViewPagerActive()) { outState.putBoolean(ISLOCKED_ARG, ((HackyViewPager) mViewPager).isLocked()); } super.onSaveInstanceState(outState); } }