Android网络:开发浏览器(六)——功能完善之查看图片属性

    UC中的图片查看属性就是列出图片所属网站的标题、URL、页面大小、页面编码以及图片的URL地址。并且,可将图片的URL和页面的URL地址复制到系统的剪贴板中。

   

    这样的话其实很容易实现,现在首先进行页面的布局编写:

   

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="#F1F1F2"

    android:orientation="vertical">

 

    <FrameLayout

       android:id="@+id/activity_pageattr_pageName"

       android:layout_width="match_parent"

       android:layout_height="wrap_content"

       android:background="@android:color/holo_blue_light">

 

        <TextView

           android:id="@+id/activity_pageattr_pageName_text"

           android:layout_width="wrap_content"

           android:layout_height="wrap_content"

           android:layout_margin="5dp"

           android:text="@string/pageAttr"

           android:textColor="@android:color/white"/>

    FrameLayout>

 

    <ScrollView

       android:id="@+id/ScrollView"

       android:layout_width="fill_parent"

       android:layout_height="fill_parent">

 

        <LinearLayout

           android:id="@+id/activity_pageattr_content"

           android:layout_width="match_parent"

           android:layout_height="wrap_content"

           android:orientation="vertical">

 

            <LinearLayout

                android:id="@+id/activity_pageattr_title"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:layout_margin="5dp"

                android:background="@drawable/attr_back"

                android:orientation="horizontal">

 

                <TextView

                    android:id="@+id/activity_pageattr_title_key"

                    android:layout_width="0dp"

                    android:layout_height="wrap_content"

                    android:layout_marginBottom="10dp"

                    android:layout_marginLeft="15dp"

                    android:layout_marginTop="10dp"

                    android:layout_weight="0.3"

                    android:text="@string/webPageTitle"

                    android:textSize="16sp"/>

 

                <TextView

                    android:id="@+id/activity_pageattr_title_value"

                    android:layout_width="0dp"

                    android:layout_height="wrap_content"

                    android:layout_marginBottom="10dp"

                    android:layout_marginRight="15dp"

                    android:layout_marginTop="10dp"

                    android:layout_weight="0.7"

                    android:textSize="16sp"/>

            LinearLayout>

 

            <RelativeLayout

                android:id="@+id/activity_pageattr_resultShowURL"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:layout_margin="5dp"

                android:background="@drawable/attr_back"

                android:visibility="gone">

 

                <TextView

                    android:id="@+id/activity_pageattr_resultShowURL_key"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_marginBottom="5dp"

                    android:layout_marginLeft="15dp"

                    android:layout_marginTop="10dp"

                    android:textSize="16sp"/>

 

                <Button

                    android:id="@+id/activity_pageattr_resultShowURL_copy"

                    android:layout_width="50dp"

                    android:layout_height="35dp"

                    android:layout_above="@+id/secondhr"

                    android:layout_alignParentRight="true"

                    android:layout_marginBottom="1.5dp"

                    android:layout_marginRight="10dp"

                    android:layout_marginTop="5dp"

                    android:background="@drawable/button"

                    android:text="@string/copy"

                    android:textColor="#777D82"

                    android:textSize="14sp"/>

 

                <View

                    android:id="@+id/secondhr"

                    android:layout_width="match_parent"

                    android:layout_height="0.5dp"

                    android:layout_below="@id/activity_pageattr_resultShowURL_key"

                    android:layout_margin="5dp"

                    android:background="#C4C4C4"/>

 

                <TextView

                    android:id="@+id/activity_pageattr_resultShowURL_value"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_below="@id/secondhr"

                    android:layout_marginBottom="10dp"

                    android:layout_marginLeft="15dp"

                    android:textSize="16sp"/>

 

                <View

                    android:id="@+id/kuozhan"

                    android:layout_width="match_parent"

                    android:layout_height="0.1dp"

                    android:layout_below="@id/activity_pageattr_resultShowURL_value"

                    android:layout_marginTop="5dp"/>

            RelativeLayout>

 

            <RelativeLayout

               android:id="@+id/activity_pageattr_currentPageUrl"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:layout_margin="5dp"

                android:background="@drawable/attr_back">

 

                <TextView

                    android:id="@+id/activity_pageattr_currentPageUrl_key"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_marginBottom="5dp"

                    android:layout_marginLeft="15dp"

                    android:layout_marginTop="10dp"

                    android:text="@string/currentPageUrl"

                    android:textSize="16sp"/>

 

                <Button

                    android:id="@+id/activity_pageattr_currentPageUrl_copy"

                    android:layout_width="50dp"

                    android:layout_height="35dp"

                    android:layout_above="@+id/hr"

                    android:layout_alignParentRight="true"

                    android:layout_marginBottom="1.5dp"

                    android:layout_marginRight="10dp"

                    android:layout_marginTop="5dp"

                    android:background="@drawable/button"

                    android:text="@string/copy"

                    android:textColor="#777D82"

                    android:textSize="14sp"/>

 

                <View

                    android:id="@+id/hr"

                    android:layout_width="match_parent"

                    android:layout_height="0.5dp"

                    android:layout_below="@id/activity_pageattr_currentPageUrl_key"

                    android:layout_margin="5dp"

                    android:background="#C4C4C4"/>

 

                <TextView

                    android:id="@+id/activity_pageattr_currentPageUrl_value"

                    android:layout_width="wrap_content"

                    android:layout_height="wrap_content"

                    android:layout_below="@id/hr"

                    android:layout_marginBottom="10dp"

                    android:layout_marginLeft="15dp"

                    android:textSize="16sp"/>

 

                <View

                    android:id="@+id/secondkuozhan"

                    android:layout_width="match_parent"

                    android:layout_height="0.1dp"

                    android:layout_below="@id/activity_pageattr_currentPageUrl_value"

                    android:layout_marginTop="5dp"/>

            RelativeLayout>

 

            <LinearLayout

                android:id="@+id/activity_pageattr_pageOtherAttr"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:layout_margin="5dp"

                android:background="@drawable/attr_back"

                android:orientation="vertical">

 

                <LinearLayout

                    android:id="@+id/activity_pageattr_pageOtherAttr_size"

                    android:layout_width="fill_parent"

                    android:layout_height="wrap_content"

                    android:layout_marginLeft="15dp"

                    android:layout_marginTop="10dp"

                    android:orientation="horizontal">

 

                    <TextView

                        android:id="@+id/activity_pageattr_pageOtherAttr_size_key"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:text="@string/pageSize"

                        android:textSize="16sp"/>

 

                    <TextView

                        android:id="@+id/activity_pageattr_pageOtherAttr_size_value"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:textSize="16sp"/>

                LinearLayout>

 

                <LinearLayout

                    android:id="@+id/activity_pageattr_pageOtherAttr_currentPageEncoding"

                    android:layout_width="fill_parent"

                    android:layout_height="wrap_content"

                    android:layout_marginBottom="10dp"

                    android:layout_marginLeft="15dp"

                    android:layout_marginTop="10dp"

                    android:orientation="horizontal">

 

                    <TextView

                        android:id="@+id/activity_pageattr_pageOtherAttr_currentPageEncoding_key"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:text="@string/currentPageEncoding"

                        android:textSize="16sp"/>

 

                    <TextView

                        android:id="@+id/activity_pageattr_pageOtherAttr_currentPageEncoding_value"

                        android:layout_width="wrap_content"

                        android:layout_height="wrap_content"

                        android:textSize="16sp"/>

                LinearLayout>

            LinearLayout>

        LinearLayout>

    ScrollView>

 

LinearLayout>

代码片段10.2.42 页面属性布局

    第一个FrameLayout的布局是为了显示一个标题,之后的ScrollView布局则是为了能够完全显示,才加上这么个滚动条视图。

    这里尤其需要注意的是:滚动条视图只能添加唯一View,所以为了正常显示,我将所有的属性条都添加到了一个LinearLayout的垂直布局当中

   

    这里有一些额外的drawable

xmlversion="1.0"encoding="utf-8"?>

<layer-listxmlns:android="http://schemas.android.com/apk/res/android">

 

    <item>

        <shapeandroid:shape="rectangle">

            <stroke

                android:width="1dp"

                android:color="#D1D1D2"/>

 

            <cornersandroid:radius="5dp"/>

        shape>

    item>

    <item

        android:left="1dp"

       android:top="1dp"

       android:right="1dp"

       android:bottom="1dp"

       >

        <shapeandroid:shape="rectangle">

            <solidandroid:color="#FFFFFF"/>

 

            <cornersandroid:radius="5dp"/>

        shape>

    item>

 

layer-list>

代码片段10.2.43 属性条背景drawable

   

    这里我用了内框和外边框,并且用了圆角,一次来展示属性条的背景。

xmlversion="1.0"encoding="utf-8"?>

<selectorxmlns:android="http://schemas.android.com/apk/res/android">

    <itemandroid:state_pressed="true"android:drawable="@drawable/button_pressed">item>

    <itemandroid:drawable="@drawable/button_normal">item>

selector>

代码片段10.2.44 button背景drawable

   

xmlversion="1.0"encoding="utf-8"?>

<layer-listxmlns:android="http://schemas.android.com/apk/res/android">

 

    <item>

        <shapeandroid:shape="rectangle">

            <stroke

                android:width="1dp"

                android:color="#D1D1D2"/>

 

            <cornersandroid:radius="2dp"/>

        shape>

    item>

    <item

       android:left="1dp"

       android:top="1dp"

       android:right="1dp"

       android:bottom="1dp"

       >

        <shapeandroid:shape="rectangle">

            <solidandroid:color="#FFFFFF"/>

        

            <cornersandroid:radius="2dp"/>

           

            <gradient

                android:endColor="#D4EAFE"

                android:startColor="#FFFFFF"

                android:angle="90"

                />

        shape>

    item>

 

layer-list>

代码片段10.2.45 button被按下时候的drawable

   

xmlversion="1.0"encoding="utf-8"?>

<layer-listxmlns:android="http://schemas.android.com/apk/res/android">

 

    <item>

        <shapeandroid:shape="rectangle">

            <stroke

                android:width="1dp"

                android:color="#D1D1D2"/>

 

            <cornersandroid:radius="2dp"/>

        shape>

    item>

    <item

       android:left="1dp"

       android:top="1dp"

       android:right="1dp"

       android:bottom="1dp"

       >

        <shapeandroid:shape="rectangle">

            <solidandroid:color="#FFFFFF"/>

        

            <cornersandroid:radius="2dp"/>

           

            <gradient

                android:startColor="#EDF1F5"

                android:endColor="#FFFFFF"

                android:angle="90"

                />

        shape>

    item>

 

layer-list>

代码片段10.2.46button的平常显示的drawable

   

    上述是button的图案样式构建,通过selector以不同的状态显示不同的形状。

        我们可以来看看预览图。

Android网络:开发浏览器(六)——功能完善之查看图片属性_第1张图片

图10.2.18   页面属性xml布局的预览

   

    基本的显示已经完成,现在就是功能实现。

   

package com.example.androidstudy_web;

 

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.webkit.WebView;

import android.widget.RelativeLayout;

import android.widget.TextView;

 

public class PageAttributesActivity extends Activity {

 

   // 网站标题

   private TextViewwebPageTitle;

   // 网页URL

   private TextViewcurrentPageUrl;

   // 网页大小

   private TextViewwebPageSize;

   // 网页编码

   private TextViewwebPageEncoding;

 

   // 特殊区域显示

   private RelativeLayoutchoosedURLLayout;

 

   // 网页信息

   private Stringtitle;

   private Stringurl;

   private Stringsize;

   private Stringencoding;

 

   // 属性类型

   private int type;

 

   @Override

   protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_pageattr);

 

      // 接受参数

      /**

       * 初始化网页信息

       *

       *


       * @param title   标题

       * @param url      url地址

       * @param size    网页大小

       * @param encoding编码

       * @param type    类型

       * */

      Bundle bundle = getIntent().getExtras();

      this.title = bundle.getString("title");

      this.url = bundle.getString("url");

      this.size = bundle.getString("size");

      this.encoding = bundle.getString("encoding");

      this.type = bundle.getInt("type");

 

      // 初始化组件

      this.init(bundle);

   }

 

   private void init(Bundle bundle) {

      this.webPageTitle = (TextView)this

            .findViewById(R.id.activity_pageattr_title_value);

      this.currentPageUrl = (TextView)this

            .findViewById(R.id.activity_pageattr_currentPageUrl_value);

      this.webPageSize = (TextView)this

            .findViewById(R.id.activity_pageattr_pageOtherAttr_size_value);

      this.webPageEncoding = (TextView)this

         .findViewById(R.id.activity_pageattr_pageOtherAttr_currentPageEncoding_value);

 

      // 设置

      this.webPageTitle.setText(this.title);

      this.currentPageUrl.setText(this.url);

      this.webPageSize.setText(this.size);

      this.webPageEncoding.setText(this.encoding);

 

      // 特殊情况判断

      if (this.type != WebView.HitTestResult.UNKNOWN_TYPE) {

         // 不为未知类型

         this.choosedURLLayout = (RelativeLayout)this

                .findViewById(R.id.activity_pageattr_resultShowURL);

         this.choosedURLLayout.setVisibility(View.VISIBLE);

 

         // 设置值

         if((this.type==WebView.HitTestResult.IMAGE_TYPE) || (this.type==WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE)){

            ((TextView) this.choosedURLLayout

                   .findViewById(R.id.activity_pageattr_resultShowURL_key))

                   .setText(getResources().getString(R.string.choosedPictureURL));

         }else if(this.type==WebView.HitTestResult.SRC_ANCHOR_TYPE){

            ((TextView) this.choosedURLLayout

                   .findViewById(R.id.activity_pageattr_resultShowURL_key))

                   .setText(getResources().getString(R.string.choosedLinkedURL));

         }

         ((TextView) this.choosedURLLayout

                .findViewById(R.id.activity_pageattr_resultShowURL_value))

                .setText(bundle.getString("typeUrl"));

      }

   }

 

}

代码片段10.2.47 页面属性的实现

   

    因为对于不同的点击类型来说,显示的是不同的,查看空白区域的属性,则只是当前页面的信息,而查看图片或是超链接则还要加上一个被选择图片链接URL或者被选择链接URL的属性条。

    因此,在设置完成基本属性后,则是要判断当前的类型type

    注意:因为没有进行缓存功能的缘故,无法查看当前页面的大小和当前页面的编码,所以我在这里并不进行页面大小和页面编码的设置。

   

    现在我们可以来实现主菜单中的点击查看图片属性的事件了

   

else if(v.getId()==R.id.item_longclicked_viewImageAttributes){

            //图片菜单-查看属性

            Intent intent = new Intent(MainActivity.this,PageAttributesActivity.class);

            Bundle bundle = new Bundle();

            bundle.putInt("type",type);

            bundle.putString("typeUrl",value);

            bundle.putString("title",title);

            bundle.putString("url",url);

            bundle.putString("size",size);

            bundle.putString("encoding",encoding);

            intent.putExtras(bundle);

            startActivity(intent);

         }

代码片段10.2.48 查看图片属性传参

   

    基本功能完成,接下来就是复制URL的地址。

   

/**

    * OnClickListener自定义继承类

    * 覆盖如下方法

    * 1. onClick

    * */

   private class ButtonOnClick implements OnClickListener{

      ClipboardManager clipboardManager = (ClipboardManager)getSystemService(CLIPBOARD_SERVICE);

      private StringtypeUrl;

     

      public ButtonOnClick(String typeUrl){

         this.typeUrl = typeUrl;

      }

     

      @Override

      public void onClick(View v) {

         if(v.getId()==R.id.activity_pageattr_currentPageUrl_copy){

            //当前页面URL的复制

            clipboardManager.setText(typeUrl);

            Log.d("clip",clipboardManager.getText().toString());

         }else if(v.getId()==R.id.activity_pageattr_resultShowURL_copy){

            //选中属性URL的复制

            clipboardManager.setText(typeUrl);

            Log.d("clip",clipboardManager.getText().toString());

         }

      }

 

   }

代码片段10.2.49 复制的操作

   

    一旦完成监听器,只需要传入需要被复制的值即可。

 

    我们可以看下效果图:

   

Android网络:开发浏览器(六)——功能完善之查看图片属性_第2张图片

图10.2.19   实际效果图

   

    当我们进行粘贴的时候,后台的信息如下:

   

图10.2.20   复制的时候的Log信息

    

案例

    AndroidStudy_web_V3.2_by 本人dddd牛仔


声明:转载请注明出处


你可能感兴趣的:(IT,Android基础)