自定义控件textview

 

自定义控件名称

public class CheyouquanLinearLayout extends ViewGroup {
  int mLeft, mRight, mTop, mBottom;
     @SuppressWarnings("rawtypes")
  Hashtable map = new Hashtable();
     public CheyouquanLinearLayout(Context context) {
         super(context);
     }

     public CheyouquanLinearLayout(Context context, int horizontalSpacing, int verticalSpacing) {
         super(context);
     }

     public CheyouquanLinearLayout(Context context, AttributeSet attrs) {
         super(context, attrs);
     }

     @SuppressLint("DrawAllocation")
  @SuppressWarnings("unchecked")
  @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
         int mWidth = MeasureSpec.getSize(widthMeasureSpec);
         int mCount = getChildCount();
         int mX = 0;
         int mY = 0;
         mLeft = 0;
         mRight = 0;
         mTop = 0;
         mBottom = 0;
         int j = 0;
         @SuppressWarnings("unused")
   View lastview = null;
         for (int i = 0; i < mCount; i++) {
             final TextView child = (TextView) getChildAt(i);
             String ss=child.getText().toString();
           
             child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
             // 此处增加onlayout中的换行判断,用于计算所需的高度
             int childw = child.getMeasuredWidth();
             int childh = child.getMeasuredHeight();
             mX += childw;  //将每次子控件宽度进行统计叠加,如果大于设定的高度则需要换行,高度即Top坐标也需重新设置


             Position position = new Position();
             mLeft = getPosition(i - j, i);
             mRight = mLeft + childw;
             if (mX >= mWidth) {
                 mX = childw;
                 mY += childh;
                 j = i;
                 mLeft = 0;
                 mRight = mLeft + child.getMeasuredWidth();
     mTop = mY;
     //PS:如果发现高度还是有问题就得自己再细调了
             }
             mBottom = mTop + child.getMeasuredHeight();
             mY = mTop;  //每次的高度必须记录 否则控件会叠加到一起
             position.left = mLeft;
             position.top = mTop;
             position.right = mRight;
             position.bottom = mBottom;
             map.put(child, position);
         }
         setMeasuredDimension(mWidth, mBottom);
     }


     @Override
     protected LayoutParams generateDefaultLayoutParams() {
         return new LayoutParams(1, 1); // default of 1px spacing
     }


     @Override
     protected void onLayout(boolean changed, int l, int t, int r, int b) {
         int count = getChildCount();
         for (int i = 0; i < count; i++) {
             View child = getChildAt(i);
             Position pos = (Position) map.get(child);
             if (pos != null) {
                 child.layout(pos.left, pos.top, pos.right, pos.bottom);
             } else {
                 Log.i("MyLayout", "error");
             }
         }
     }


     private class Position {
         int left, top, right, bottom;
     }


     public int getPosition(int IndexInRow, int childIndex) {
         if (IndexInRow > 0) {
             return getPosition(IndexInRow - 1, childIndex - 1)
                     + getChildAt(childIndex - 1).getMeasuredWidth();
         }
         return getPaddingLeft();
     }

 

动态加载人名

 // 赞的人名
   DisplayMetrics metric = new DisplayMetrics(); 
   context.getWindowManager().getDefaultDisplay().getMetrics(metric);
      //dp转化成px
   float scale = context.getResources().getDisplayMetrics().density;
   int widthQi=(int) (125*scale + 0.5f);
   int widthQuan = metric.widthPixels-widthQi; //获取屏幕的宽度
   float widthQ=0;
   for (int a = 0; a < zList.size(); a++) {
    String name=zList.get(a).getNick_name();
    final String userid=zList.get(a).getUser_id();
    if(a!=0){
     TextView tv_dun=new TextView(context);
     tv_dun.setText(",");
     tv_dun.setTextColor(Color.rgb(20, 150, 193));
     viewHolder.tv_zan_name.addView(tv_dun);
     int wName2 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int hName2 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        tv_dun.measure(wName2,hName2);
        widthQ=tv_dun.getMeasuredWidth()+widthQ;
    }
    /***************以下为换行操作start**********************/
    //获取数据宽度
       TextView tvName=new TextView(context);
       tvName.setText(name);
       tvName.setTextSize(14);
    int wgName = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
       int hgName = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
       tvName.measure(wgName, hgName);
       //获取字符串的长度
    int widthName=tvName.getMeasuredWidth();//px
    //获取前面的本行的所有宽度
    float widthD=widthQ;
    widthQ=widthName+widthQ;
    if(widthQ<=widthQuan){
     TextView tv=new TextView(context);
     tv.setText(name);
     tv.setTextSize(14);
     tv.setTextColor(Color.rgb(20, 150, 193));
     viewHolder.tv_zan_name.addView(tv);
     tv.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
       Intent intent = new Intent(context, MyPhotoActivity.class);
       intent.putExtra("friends_id",userid);
       context.startActivity(intent);
      }
     });
    }else{
     float widths=widthQuan-widthD;
     String b="";
     int wss=0;
     for(int h=0;h<name.length();h++){
      b=b+name.charAt(h);
      
      TextView tvName2=new TextView(context);
      tvName2.setText(b);
      tvName2.setTextSize(14);
      int wName2 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
         int hName2 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
         tvName2.measure(wName2,hName2);
         //获取字符串的长度
      int wsName2=tvName2.getMeasuredWidth();//px
      if(wsName2>widths){
       wss=h-1;
       break;
      }else if(wsName2==widths){
       wss=h;
       break;
      }
     }
     /**一个textview拆成两个textview*/
     String name1=name.substring(0, wss+1);
     String name2=name.substring(wss+1,name.length());
     if(!TextUtils.isEmpty(name1)){
      TextView tv=new TextView(context);
      tv.setText(name1);
      tv.setTextSize(14);
      tv.setTextColor(Color.rgb(20, 150, 193));
      viewHolder.tv_zan_name.addView(tv);
      tv.setOnClickListener(new OnClickListener() {
       
       @Override
       public void onClick(View v) {
        Intent intent = new Intent(context, MyPhotoActivity.class);
        intent.putExtra("friends_id",userid);
        context.startActivity(intent);       
       }
      });
     }
     if(!TextUtils.isEmpty(name2)){
      TextView tv=new TextView(context);
      tv.setText(name2);
      tv.setTextSize(14);
      tv.setEllipsize(TruncateAt.END);
      tv.setTextColor(Color.rgb(20, 150, 193));
      viewHolder.tv_zan_name.addView(tv);
      tv.setOnClickListener(new OnClickListener() {
       @Override
       public void onClick(View v) {
        Intent intent = new Intent(context, MyPhotoActivity.class);
        intent.putExtra("friends_id",userid);
        context.startActivity(intent);       }
      });
     }
     TextView tv6=new TextView(context);
     tv6.setText(name2);
     tv6.setTextSize(14);
     int wg6 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        int hg6 = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
        tv6.measure(wg6, hg6);
        //获取字符串的长度
     int w2Px=tv6.getMeasuredWidth();//px
     widthQ=w2Px;
     /*************以上为换行操作end*******************/
    }
   }
  

 

 

题目描述 牛牛和她的朋友们正在玩一个有趣的游戏,他们需要构建一个有 $n$ 个节点的无向图,每个节点都有一个唯一的编号并且编号从 $1$ 到 $n$。他们需要从节点 $1$ 到节点 $n$ 找到一条最短路径,其中路径长度是经过的边权的和。为了让游戏更有趣,他们决定在图上添加一些额外的边,这些边的权值都是 $x$。他们想知道,如果他们添加的边数尽可能少,最短路径的长度最多会增加多少。 输入格式 第一行包含两个正整数 $n$ 和 $m$,表示节点数和边数。 接下来 $m$ 行,每行包含三个整数 $u_i,v_i,w_i$,表示一条无向边 $(u_i,v_i)$,权值为 $w_i$。 输出格式 输出一个整数,表示最短路径的长度最多会增加多少。 数据范围 $2 \leq n \leq 200$ $1 \leq m \leq n(n-1)/2$ $1 \leq w_i \leq 10^6$ 输入样例 #1: 4 4 1 2 2 2 3 3 3 4 4 4 1 5 输出样例 #1: 5 输入样例 #2: 4 3 1 2 1 2 3 2 3 4 3 输出样例 #2: 2 算法 (BFS+最短路) $O(n^3)$ 我们把问题转化一下,假设原图中没有添加边,所求的就是点 $1$ 到点 $n$ 的最短路,并且我们已经求出了这个最短路的长度 $dis$。 接下来我们从小到大枚举边权 $x$,每次将 $x$ 加入图中,然后再次求解点 $1$ 到点 $n$ 的最短路 $dis'$,那么增加的最短路长度就是 $dis'-dis$。 我们发现,每次加入一个边都需要重新求解最短路。如果我们使用 Dijkstra 算法的话,每次加入一条边需要 $O(m\log m)$ 的时间复杂度,总的时间复杂度就是 $O(m^2\log m)$,无法通过本题。因此我们需要使用更优秀的算法。 观察到 $n$ 的范围比较小,我们可以考虑使用 BFS 求解最短路。如果边权均为 $1$,那么 BFS 可以在 $O(m)$ 的时间复杂度内求解最短路。那么如果我们只是加入了一条边的话,我们可以将边权为 $x$ 的边看做 $x$ 条边的组合,每次加入该边时,我们就在原始图上添加 $x$ 条边,边权均为 $1$。这样,我们就可以使用一次 BFS 求解最短路了。 但是,我们不得不考虑加入多条边的情况。如果我们还是将边权为 $x$ 的边看做 $x$ 条边的组合,那么我们就需要加入 $x$ 条边,而不是一条边。这样,我们就不能使用 BFS 了。 但是,我们可以使用 Floyd 算法。事实上,我们每次加入边时,只有边权等于 $x$ 的边会发生变化。因此,如果我们枚举边权 $x$ 时,每次只需要将边权等于 $x$ 的边加入图中,然后使用 Floyd 算法重新计算最短路即可。由于 Floyd 算法的时间复杂度为 $O(n^3)$,因此总的时间复杂度为 $O(n^4)$。 时间复杂度 $O(n^4)$ 空间复杂度 $O(n^2)$ C++ 代码 注意点:Floyd算法计算任意两点之间的最短路径,只需要在之前的路径基础上加入新的边构成的新路径进行更新即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值